[英]How to add smooth scroll to your website using CSS or Javascript?
I need to add a smooth scroll effect everytime a link in my navbar is clicked.每次单击导航栏中的链接时,我都需要添加平滑的滚动效果。 Here's the navbar:这是导航栏:
<header class="navbar">
<div class="navElements">
<div class="logo">
<img src="./logo.png" alt="Millennium" />
</div>
<div class="hamburger">
<input type="checkbox" id="check" />
<label for="check" class="checkButton">
<i class="fas fa-bars" style="font-size: 35px"></i>
</label>
</div>
</div>
<div>
<ul class="navItems">
<li><a href="#home" class="navItem active">HOME</a></li>
<li><a href="#services" class="navItem">SERVICES</a></li>
<li><a href="#projects" class="navItem">PROJECTS</a></li>
<li><a href="#ourTeam" class="navItem">OUR TEAM</a></li>
<li><a href="#contact" class="navItem">CONTACT</a></li>
</ul>
</div>
</header>
What do I need to add?我需要添加什么? And to which class?哪个 class? I have added id
for every section
in the body
我为body
中的每个section
添加了id
<div id="home">Stuff</div>
<div id="services">Stuff</div>
<div id="projects">Stuff</div>
<div id="ourTeam">Stuff</div>
<div id="contacts">Stuff</div>
Help me out...帮帮我...
There's a CSS rule:有一个 CSS 规则:
scroll-behavior: smooth;
Can be applied to a scrollable element可以应用于可滚动元素
You need to animate the Html, body
Try like this hope it works!您需要为Html, body
尝试这样希望它有效!
HTML link: HTML 链接:
<a href="#yourId" class="scrollTo">Learn More</a>
JS: JS:
$(".scrollTo").on('click', function(e) {
e.preventDefault();
var target = $(this).attr('href');
$('html, body').animate({
scrollTop: ($(target).offset().top)
}, 2000);
});
HTML anchor: HTML 锚:
<div id="yourId">My content here</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.