$('a[href^="#"]').on('click', function(event) { var target = $(this.getAttribute('href')); if (target.length) { event.preventDefault(); $('html, body').stop().animate({ scrollTop: target.offset().top }, 1000); } });补全HTML+CSS代码
时间: 2024-02-06 08:03:25 浏览: 125
js 点击a标签 获取a的自定义属性方法
好的,以下是一个简单的HTML和CSS代码示例,用于演示平滑滚动到页面顶部的jQuery效果:
HTML代码:
```
<!DOCTYPE html>
<html>
<head>
<title>Smooth Scroll</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<header>
<nav>
<ul>
<li><a href="#about">About</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#contact">Contact Us</a></li>
</ul>
</nav>
</header>
<main>
<section id="about">
<h2>About Us</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec bibendum, nisi vel bibendum vestibulum, turpis ex sodales ipsum, a bibendum urna ex nec tellus.</p>
</section>
<section id="services">
<h2>Our Services</h2>
<p>Nullam nec mi semper, pulvinar mi eu, aliquet felis. Integer vel odio eget odio tempus condimentum.</p>
</section>
<section id="contact">
<h2>Contact Us</h2>
<p>Phasellus vel metus vel felis dignissim ullamcorper. Etiam nec nulla sed nibh finibus luctus.</p>
</section>
</main>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
header {
background-color: #333;
color: #fff;
height: 60px;
padding: 10px;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
}
nav ul li {
display: inline-block;
margin-right: 20px;
}
nav ul li a {
color: #fff;
text-decoration: none;
}
main {
padding: 50px;
}
h2 {
font-size: 36px;
margin-bottom: 20px;
}
p {
font-size: 18px;
line-height: 1.5;
margin-bottom: 30px;
}
```
注意事项:
- 代码中引入了jQuery库,需要在页面中先引入才能使用。
- CSS代码仅做简单的样式设置,需要根据实际需求进行修改。
阅读全文