JS函数封装怎样实现,代码是什么
Admin 2022-11-15 群英技术资讯 675 次浏览
<!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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了vue3.0 自适应不同分辨率电脑的操作,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
这篇文章我们来了解jQuery修改行内样式的方法,改变行内样式并不难,这里我们使用attr()就能实现,下面我们通过实例来详细的了解看看吧,有需要的朋友可以参考。
这篇文章主要介绍了JavaScript二叉搜索树构建操作详解,文章围绕主题展开二叉搜索树的性质以及二叉搜索树的构建、查找和删除等内容,需要的小伙伴可以参考一下
这篇文章主要给大家介绍了关于Node.js中异步生成器与异步迭代的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
目录前言一、 toString()二、join()三、 JSON.stringify()四、sort()五、filter()总结前言在 js 中是不能直接用 == 或者 === 来比较两个数组是否相等,那就需要对数组的值进行比较。下面各种方法,要根据具体情况来使用。一、 toString()当两个数组元素类型相同,顺序
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008