JavaScript特效!scroll与缓动动画

本文笔记基于「千古壹号」的GitHub项目:https://github.com/qianguyihao/web

1.基础

  • 原理:盒子未来位置=当前位置+步长,步长会越来越小。设置点击事件,事件函数为定时器。
 div.style.left = div.offsetLeft + (400 - div.offsetLeft) / 10 + "px";
  • 封装函数:与[匀速动画构造类似,但是步长原理不同,匀速的是固定的,而缓动会随当前值发生变化;
function animate(ele, target) {
   
		//1.清除定时器并设置定时器函数
		 clearInterval(ele.timer);
        ele.timer = setInterval(function () {
   
        //2.处理步长
            var step = (target - ele.offsetLeft) / 10;
            step = step > 0 ? Math.ceil(step) : Math.floor(step);
		//3.if函数判断当前位置
            if (Math.abs(target - ele.offsetLeft) <= Math.abs(step
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是几个基于jQuery实现动画或者网页特效的案例: 1. 图片轮播效果 HTML结构: ```html <div class="slider"> <div class="slider-wrapper"> <img src="image1.jpg" alt="Image 1" /> <img src="image2.jpg" alt="Image 2" /> <img src="image3.jpg" alt="Image 3" /> </div> </div> ``` CSS样式: ```css .slider { position: relative; width: 100%; height: 400px; overflow: hidden; } .slider-wrapper { position: absolute; width: 300%; height: 100%; left: 0; top: 0; } .slider-wrapper img { width: 33.333%; height: 100%; float: left; display: block; } ``` JavaScript代码: ```javascript $(function() { var sliderWrapper = $('.slider-wrapper'); var sliderImageWidth = $('.slider-wrapper img').width(); var sliderImageCount = $('.slider-wrapper img').length; var currentImage = 1; sliderWrapper.css('width', sliderImageCount * sliderImageWidth); function slide() { sliderWrapper.animate({left: -sliderImageWidth * currentImage}, function() { currentImage++; if(currentImage === sliderImageCount) { currentImage = 1; sliderWrapper.css('left', 0); } }); } setInterval(slide, 3000); }); ``` 2. 折叠面板效果 HTML结构: ```html <div class="accordion"> <div class="accordion-section"> <h3 class="accordion-header">Section 1</h3> <div class="accordion-content"> <p>Content for section 1</p> </div> </div> <div class="accordion-section"> <h3 class="accordion-header">Section 2</h3> <div class="accordion-content"> <p>Content for section 2</p> </div> </div> <div class="accordion-section"> <h3 class="accordion-header">Section 3</h3> <div class="accordion-content"> <p>Content for section 3</p> </div> </div> </div> ``` CSS样式: ```css .accordion-section { border: 1px solid #ccc; margin-bottom: 10px; } .accordion-header { padding: 10px; margin: 0; background-color: #f0f0f0; cursor: pointer; } .accordion-content { display: none; padding: 10px; } ``` JavaScript代码: ```javascript $(function() { var accordionHeaders = $('.accordion-header'); accordionHeaders.click(function() { var accordionContent = $(this).next('.accordion-content'); if(accordionContent.is(':visible')) { accordionContent.slideUp(); } else { accordionContent.slideDown(); } }); }); ``` 3. 滚监听效果 HTML结构: ```html <nav class="menu"> <ul> <li><a href="#section1">Section 1</a></li> <li><a href="#section2">Section 2</a></li> <li><a href="#section3">Section 3</a></li> </ul> </nav> <section id="section1"> <h2>Section 1</h2> <p>Content for section 1</p> </section> <section id="section2"> <h2>Section 2</h2> <p>Content for section 2</p> </section> <section id="section3"> <h2>Section 3</h2> <p>Content for section 3</p> </section> ``` CSS样式: ```css .menu { position: fixed; top: 0; left: 0; } .menu ul { list-style: none; margin: 0; padding: 0; } .menu li { display: inline-block; margin-right: 10px; } section { height: 500px; margin-top: 60px; border: 1px solid #ccc; padding: 20px; } ``` JavaScript代码: ```javascript $(function() { var menu = $('.menu'); var menuLinks = $('.menu a'); var sections = $('section'); $(window).scroll(function() { var currentScroll = $(this).scrollTop(); sections.each(function() { var sectionTop = $(this).offset().top - 100; var sectionBottom = sectionTop + $(this).height(); if(currentScroll >= sectionTop && currentScroll <= sectionBottom) { var sectionId = $(this).attr('id'); var menuLink = menu.find('a[href="#' + sectionId + '"]'); menuLinks.removeClass('active'); menuLink.addClass('active'); } }); }); }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值