Click on the anchor link in JS to scroll smoothly and adjust to the top position freely

Click on the anchor link in JS to scroll smoothly and adjust to the top position freely

Click on the anchor link to scroll smoothly and adjust freely to the top position

1. Add anchor points. Note that the a tag uses nacTo instead of href.

<ul>
	<li><a class="anchor" navTo="one">Target selection</a></li>
	<li><a class="anchor" navTo="two">Target customers</a></li>
	<li><a class="anchor" navTo="three">Advertising Format</a></li>
	<li><a class="anchor" navTo="four">Advertising</a></li>
	<li><a class="anchor" navTo="five">Budget and schedule</a></li>
	<li><a class="anchor" navTo="six">Conversion Tracking</a></li>
</ul>

2. Add the corresponding id to the content that needs to be scrolled on the right

<div class="right">
			<div id="one">
				<div class="target">
					<i>Your goal is to:</i>
					<div class="brand_engagement"><img src="img/signal.png" /><i>Brand Engagement</i><b>Change</b></div>
					<p>I want people to know about my business. </p>
					<div class="advertising_that">
						<img src="img/right.png" /> Your ad will be shown to the right people most likely to view your ad. </b>
					</div>
				</div>
			</div>
			<div id="two">
				The second part of the content </div>
			<div id="three">
				The third part of the content </div>
			<div id="four">
				The fourth part of the content </div>
			<div id="five">
				The fifth part of the content </div>
		</div>

3. Add js to smoothly scroll to the top distance (150 is the height of the navigation, which can be adjusted freely as needed)

$('.anchor').click(function () {
		var navto = $(this).attr('navto');
		if (navto != "#") {
			var $div = $('#' + navto);
			var top = $div.offset().top || 0;
			$('html,body').animate({
				'scroll-top': top - 150
			}, 500);
		} else {
			$('html,body').animate({
				'scroll-top': 0
			}, 500);
		}
	});

OK~ Effect display

insert image description here

This is the end of this article about smooth scrolling when clicking on anchor links in JS and freely adjusting to the top position. For more relevant js anchor link smooth scrolling content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue.js practice: implementing dynamic anchor points by listening to scroll events
  • JS implements the method of placing the mouse on the link to display scrolling text
  • Link horizontal text up and down gap loop scroll JS effect
  • Link image seamless (uninterrupted) smooth scrolling to the left Js version code

<<:  Detailed explanation of eight ways to optimize MySQL database (classic must-read)

>>:  Tutorial on configuring and using i3 window manager in Linux

Recommend

MySQL 8.0.15 download and installation detailed tutorial is a must for novices!

This article records the specific steps for downl...

MySQL 5.7.18 Installer installation download graphic tutorial

This article records the detailed installation tu...

A good way to improve your design skills

So-called talent (left brain and right brain) Tha...

XHTML tags have a closing tag

<br />Original link: http://www.dudo.org/art...

How to use environment variables in nginx configuration file

Preface Nginx is an HTTP server designed for perf...

Three ways to copy MySQL tables (summary)

Copy table structure and its data The following s...

How to use macros in JavaScript

In languages, macros are often used to implement ...

What to do if the online MySQL auto-increment ID is exhausted

Table of contents Table definition auto-increment...

Solution to the error when installing Docker on CentOS version

1. Version Information # cat /etc/system-release ...

Detailed steps and problem solving methods for installing MySQL 8.0.19 on Linux

I recently bought a Tencent Cloud server and buil...

HTML introductory tutorial HTML tag symbols quickly mastered

Side note <br />If you know nothing about HT...

Sharing of SVN service backup operation steps

SVN service backup steps 1. Prepare the source se...

MySQL login and exit command format

The command format for mysql login is: mysql -h [...