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 improve MySQL Limit query performance

In MySQL database operations, we always hope to a...

Detailed tutorial on deploying Springboot or Nginx using Kubernetes

1 Introduction After "Maven deploys Springbo...

Use a few interview questions to look at the JavaScript execution mechanism

Table of contents Previous words Synchronous and ...

Vue integrates PDF.js to implement PDF preview and add watermark steps

Table of contents Achieve results Available plugi...

Vue realizes the card flip effect

This article example shares the specific code of ...

How to change the encoding of MySQL database to utf8mb4

The utf8mb4 encoding is a superset of the utf8 en...

How to deploy kafka in docker

Table of contents 1. Build Docker 2. Enter the co...

Modify file permissions (ownership) under Linux

Linux and Unix are multi-user operating systems, ...

MySQL 8.0.12 winx64 decompression version installation graphic tutorial

Recorded the installation of mysql-8.0.12-winx64 ...

JavaScript recursion detailed

Table of contents 1. What is recursion? 2. Solve ...

Seven Principles of a Skilled Designer (2): Color Usage

<br />Previous article: Seven Principles of ...

How to allow all hosts to access mysql

1. Change the Host field value of a record in the...

The difference between absolute path and relative path in web page creation

1. Absolute path First of all, on the local compu...