To scroll to a specific element after clicking on a div with a specific class using the GSAP library, you can follow these steps:

  1. Include the GSAP library in your HTML file by adding the following script tag in the head or body section:
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.6.1/gsap.min.js"></script>
  1. Add an event listener to the div element with the desired class to trigger the scroll animation. For example, if the class of the div is "scroll-button", you can use the following code:
<div class="scroll-button">Click to Scroll</div>

<script>
  // Get the scroll-button div element
  var scrollButton = document.querySelector('.scroll-button');

  // Add click event listener
  scrollButton.addEventListener('click', function() {
    // Scroll to the target element using GSAP
    gsap.to(window, {duration: 1, scrollTo: {y: '.target-element', offsetY: 50}});
  });
</script>
  1. Replace ".target-element" with the selector of the element you want to scroll to. The "offsetY" property can be adjusted to set the offset from the top of the target element.

Make sure to replace the "scroll-button" class and ".target-element" selector with your own class and selector.

Note: The code above assumes you have already added the necessary CSS styles for the scroll button and the target element

gsapminjs i want to scroll after click on div class

原文地址: https://www.cveoy.top/t/topic/hM9F 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录