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

How to set list style attributes in CSS (just read this article)

List style properties There are 2 types of lists ...

CSS3 speeds up and delays transitions

1. Use the speed control function to control the ...

Linux bridge method steps to bridge two VirtualBox virtual networks

This article originated from my complaints about ...

Vue3.0 project construction and usage process

Table of contents 1. Project construction 2: Dire...

How to implement concurrency control in JavaScript

Table of contents 1. Introduction to Concurrency ...

Learn the basics of JavaScript DOM operations in one article

DOM Concepts DOM: document object model: The docu...

Some ways to solve the problem of Jenkins integrated docker plugin

Table of contents background Question 1 Error 2 E...

js to implement verification code interference (static)

This article shares the specific code of js to im...

Share some uncommon but useful JS techniques

Preface Programming languages ​​usually contain v...

JavaScript implements countdown on front-end web page

Use native JavaScript to simply implement the cou...

MySQL implementation of lastInfdexOf function example

Sometimes MySQL needs to use a function similar t...

jQuery implements form validation

Use jQuery to implement form validation, for your...

Specific usage of fullpage.js full screen scrolling

1.fullpage.js Download address https://github.com...

MySQL 5.7.21 installation and configuration tutorial

The simple installation configuration of mysql5.7...