JS函数封装怎样实现,代码是什么
Admin 2022-11-15 群英技术资讯 1194 次浏览
在这篇文章中,我们来学习一下“JS函数封装怎样实现,代码是什么”的相关知识,下文有详细的讲解,易于大家学习和理解,有需要的朋友可以借鉴参考,下面就请大家跟着小编的思路一起来学习一下吧。
<!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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
axios怎样实现请求封装及使用?通常,如果我们是做大型的项目,那么就需要使用到很多接口,而为了使用的方便,将接口封装起来很一个不错的方法,因此下面小编就能给大家分享axios的简单封装,需要的朋友可以参考。
这篇文章主要为大家详细介绍了js实现列表自动滚动循环播放,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
对于留言板想必大家应该都不陌生,而添加删除功能是留言板的基础功能,因此这篇文章就主要给大家分享是的用jquery实现留言板添加和删除功能的代码,感兴趣的朋友可以参考,下面我们就来具体了解看看吧。
本文主要介绍了element-plus中如何实现按需导入与全局导入,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要为大家详细介绍了JavaScript实现Tab栏切换特效,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008