html锚点 缓动,最简单的jquery页内锚点平滑跳转代码

通过jQuery实现页面内锚点平滑跳转的方法很多,可以通过插件实现,也可以简单的通过animate方法实现,下面介绍比较简单的方法。

代码只有一句话$("html,body").animate({scrollTop: $("#elementid").offset().top}, 1000);

animate()方法用来实现一组css的自定义动画,有两种调用方法

第一种形式接受4个参数,如下所示

.animate( properties [, duration] [, easing] [, complete] )

properties – 一个包含样式属性及值的映射

duration – 可选的速度参数,既可以是预置的字符串,也可以是一个毫秒数值

easing – 可选的缓动类型,jquery默认的只有两种:swing和linear,要使用其它效果需要安装缓动类的插件

complete – 可选的回调函数,在动画结束时被调用

第一种形式的示例如下.animate({property1: 'value1', property2: 'value2'},

speed, easing, function() {

alert('The animation is finished.');

});

本文实现锚点跳转的代码使用了第一种形式

$("html,body")代表对html或body元素进行动画,即要改变他们的css属性值

scrollTop是要改变的属性值,表示滚动条滑过的距离,在这里表示向下拉动浏览器滚动条后html(body)被浏览器顶端隐藏的高度

$("#elementid").offset().top就是html(body)需要被浏览器顶端隐藏的高度,它代表id为elementid的元素顶端到网页顶端(不是浏览器可视区域顶端)的绝对距离。

1000是指动画时间为1秒

animate()方法还有第二种调用形式

.animate( properties, options )

一个是属性映射,一个是选项映射。实际上这里的第二个参数是把第一种形式的第2-4个参数封装在另一个映射中,同时又添加了两个选项。第二种形式的代码如下:.animate({

property1: 'value1',

property2: 'value2'

}, {

duration: 'value',

easing: 'value',

complete: function(){

alert('The animation is finished.');

},

queue: boolen,

step: callback

});

好的,以下是一个简单HTML+CSS+jQuery实现平滑动画的代码示例: HTML代码: ``` <!DOCTYPE html> <html> <head> <title>Smooth Animation</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> <div class="service"> <img src="service1.jpg" alt="Service 1"> <p>Service 1 Description</p> </div> <div class="service"> <img src="service2.jpg" alt="Service 2"> <p>Service 2 Description</p> </div> <div class="service"> <img src="service3.jpg" alt="Service 3"> <p>Service 3 Description</p> </div> </section> <section id="contact"> <h2>Contact Us</h2> <form action="#" method="post"> <label for="name">Name:</label> <input type="text" id="name" name="name" required> <label for="email">Email:</label> <input type="email" id="email" name="email" required> <label for="message">Message:</label> <textarea id="message" name="message" required></textarea> <button type="submit">Send Message</button> </form> </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; } .service { display: inline-block; margin-right: 20px; vertical-align: top; width: 200px; } .service img { width: 100%; } form label, form input, form textarea { display: block; margin-bottom: 10px; width: 100%; } form input, form textarea { padding: 10px; } form button { background-color: #333; border: none; color: #fff; cursor: pointer; padding: 10px; } form button:hover { background-color: #444; } .hide { display: none; } ``` jQuery代码: ``` $(document).ready(function() { // 平滑滚动到锚点 $('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); } }); // 滚动到服务区域时显示服务动画 $(window).on('scroll', function() { var servicesTop = $('#services').offset().top; var servicesHeight = $('#services').height(); var scrollTop = $(this).scrollTop(); var windowHeight = $(this).height(); if (scrollTop + windowHeight > servicesTop && scrollTop < servicesTop + servicesHeight) { $('.service').addClass('animated fadeInUp'); } else { $('.service').removeClass('animated fadeInUp'); } }); // 表单提交成功后显示提示信息 $('form').on('submit', function(event) { event.preventDefault(); $('.success').removeClass('hide'); }); }); ``` 注意事项: - 代码中引入了jQuery和animate.css库,需要在页面中先引入才能使用。 - animate.css是一个开源的CSS动画库,用于为HTML元素添加动画效果。在本例中使用了其中的fadeInUp动画效果。 - CSS代码仅做简单的样式设置,需要根据实际需求进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值