Home >Web Front-end >CSS Tutorial >How to Jump or Scroll to a Specific Position on Page Click with jQuery?
Jump or Scroll to a Specific Position on Page Click
In web development, it's sometimes necessary to jump down or scroll to a specific div or target on a page when a button is clicked. This can be achieved using jQuery, a popular JavaScript library for simplifying front-end development.
To scroll or jump to a certain position, div, or target, you can use jQuery's animate() method. This method allows you to animate the scroll position by providing the target position as an argument.
Here's how you can achieve this functionality:
<code class="javascript">$(function() { $('#clickMe').click(function() { $('html, body').animate({ scrollTop: ($('#target').offset().top - 100) }, 600); }); });</code>
In this example:
This script will ensure that when you click the button with the ID "clickMe," the page will smoothly scroll or jump to the element with the ID "target."
The above is the detailed content of How to Jump or Scroll to a Specific Position on Page Click with jQuery?. For more information, please follow other related articles on the PHP Chinese website!