To achieve this, you can use the jQuery animate function to scroll to the specified div class after a click event. Here's an example:

HTML:

<button id="scrollButton">Scroll to Specific Div</button>
<div class="scrollToDiv">Div 1</div>
<div class="scrollToDiv">Div 2</div>
<div class="scrollToDiv">Div 3</div>

CSS:

.scrollToDiv {
  height: 300px;
  margin-bottom: 20px;
  background-color: lightblue;
}

JavaScript (jQuery):

$(document).ready(function() {
  $('#scrollButton').click(function() {
    var targetDiv = $('.scrollToDiv').eq(2); // Scroll to the third div (index 2)
    var targetOffset = targetDiv.offset().top;

    $('html, body').animate({
      scrollTop: targetOffset
    }, 1000);
  });
});

In this example, when the button with the ID scrollButton is clicked, the page will scroll to the third div with the class scrollToDiv. The eq(2) method is used to select the third div (index 2) since jQuery uses a zero-based index. The offset().top function is used to get the top position of the target div. Finally, the animate function is used to smoothly scroll to the target div over a duration of 1000 milliseconds (1 second)

i use locomotive scroll & i want to scroll after click on spesfic div class with jquery lang

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

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