JS函数封装怎样实现,代码是什么
Admin 2022-11-15 群英技术资讯 608 次浏览
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box1 { width: 30px; height: 30px; background-color: pink; position: absolute; top: 100px; right: 0px; z-index: 1; } .box2 { width: 140px; height: 30px; background-color: purple; position: absolute; top: 100px; right: -140px; } .box { width: 400px; height: 1000px; border: 1px solid grey; position: relative; overflow: hidden; } </style> </head> <body> <div class="box"> <div class="box1">^</div> <div class="box2">会员内容</div> </div> <script> //鼠标经过box1的时候,box2就往左边移140px; var box1 = document.querySelector('.box1') var box2 = document.querySelector('.box2') var a = box2.offsetLeft box1.addEventListener('mouseover', function() { animate(box2, a - 140) }) box1.addEventListener('mouseout', function() { animate(box2, a + 140) }) function animate(obj, target, callback) { clearInterval(obj.timer) //先把原先地定时器清除之后,再开启另外一个新地定时器 obj.timer = setInterval(fn, [15]) function fn() { var a = obj.offsetLeft //不能换成div.style.left 不然会只移动一次。注意读取位置永offset,修改永style var step = (target - a) / 10 step = step > 0 ? Math.ceil(step) : Math.floor(step) //将结果赋值回去 //把步长值改为整数,不要出现小数的情况 if (a == target) { //取消定时器 clearInterval(obj.timer) //执行回调函数 函数名+()回调函数写到定时器结束里面 //首先判断没有有这个回调函数 if (callback) { callback() } } obj.style.left = a + step + 'px' } } //鼠标离开的时候,box2就往右边移140px </script> </body> </html>
这个下面看着就头晕
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box1 { width: 30px; height: 30px; background-color: pink; position: absolute; top: 100px; right: 0px; z-index: 1; } .box2 { width: 140px; height: 30px; background-color: purple; position: absolute; top: 100px; right: -140px; } .box { width: 400px; height: 1000px; border: 1px solid grey; position: relative; overflow: hidden; } </style> <script src="animater.js"></script> </head> <body> <div class="box"> <div class="box1">^</div> <div class="box2">会员内容</div> </div> <script> //鼠标经过box1的时候,box2就往左边移140px; var box1 = document.querySelector('.box1') var box2 = document.querySelector('.box2') var a = box2.offsetLeft box1.addEventListener('mouseover', function() { animate(box2, a - 110) }) box1.addEventListener('mouseout', function() { animate(box2, a + 110) }) </script> </body> </html>
先将js单独写在一个独立的文件中。
之后直接使用函数。但在此之前要先引入JS文件
<script> //鼠标经过box1的时候,box2就往左边移140px; var box1 = document.querySelector('.box1') var box2 = document.querySelector('.box2') var img = document.querySelector('img') var a = box2.offsetLeft box1.addEventListener('mouseover', function() { animate(box2, a - 110, callback) }) box1.addEventListener('mouseout', function() { animate(box2, a + 110, callback1) }) </script>
JS单独文件:
function animate(obj, target, callback) { clearInterval(obj.timer) //先把原先地定时器清除之后,再开启另外一个新地定时器 obj.timer = setInterval(fn, [15]) function fn() { var a = obj.offsetLeft //不能换成div.style.left 不然会只移动一次。注意读取位置永offset,修改永style var step = (target - a) / 10 step = step > 0 ? Math.ceil(step) : Math.floor(step) //将结果赋值回去 //把步长值改为整数,不要出现小数的情况 if (a == target) { //取消定时器 clearInterval(obj.timer) //执行回调函数 函数名+()回调函数写到定时器结束里面 //首先判断没有有这个回调函数 if (callback) { callback() } } obj.style.left = a + step + 'px' } } function callback() { img.src = '10-右.png' img.style.width = '50%' } function callback1() { img.src = '9-左.png' img.style.width = '50%' }
觉得在图标不是很多的时候用iconfont要方便很多。单数如果图标很多,就用lcoMoon来导入图标。或者使用精灵图等来设置
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
目录正文这里有两个方法方法一:这个比较集中方法二:cookie存取各封装成函数正文在京东上浏览的时候,发现了一个比较人性化的小功能,浏览商品,浏览到一半的时候,如下图所示:我重新加载网页,刷新之后,滚动条依然定位在我刚刚浏览的位置,这个小功能感觉还不错,挺方便的。具体是怎么实现的呢,去网上大概查了一下。这个是使用滚动条
node.js中有中文乱码怎么办?很多朋友都有遇到过node.Js中出现中文乱码的问题,那么遇到这样的情况我们该怎样解决呢?下面就给大家来分享一个方法,需要的朋友可以参考。
这篇文章主要给大家介绍了关于Vue中ref和$refs使用方法的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章给大家分享的是JavaScript中escape函数的相关内容。在javascript中,escape()方法用于对字符串进行编码,小编觉得挺实用的,因此分享给大家做个参考,文中的示例代码介绍得很详细,有需要的朋友可以参考,接下来就跟随小编一起学习一下吧。
基于element-plus的二次封装数据双向绑定在实际开发中,经常需要基于element-plus封装一些自己的定制化组件,方便快速构建我们当前的业务。在vue2.0中父子组件数据的双向绑定通
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008