每天一个jquery插件-滑动的底边
滑动的底边
还是B站里面的特效,我寻思这个应该很简单,然后动手搞了一下,还不错
目标效果与我的效果
代码部分注释在里面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>滑动的底边</title>
<script src="js/jquery-3.4.1.min.js"></script>
<style>
*{
margin: 0px;
padding: 0px;
user-select: none;
}
#menu{
border: 1px solid lightgray;
display: flex;
width: 600px;
margin: 0 auto;
position: relative;
}
.item{
min-width: 100px;
height: 30px;
display: flex;
justify-content: center;
align-items: center;
font-size: 12px;
cursor: pointer;
}
.item:hover{
color: blue;
}
#rel{
height:5px;
width: 600px;
position: absolute;
bottom: -5px;
}
.pos{
background-color: blue;
width: 100px;
height: 5px;
margin-left:0;
}
</style>
</head>
<body>
<div id="menu">
<div class="item">菜单1</div>
<div class="item">菜单2</div>
<div class="item">菜单3</div>
<div class="item">菜单4</div>
<div class="item">菜单5</div>
<div class="item">菜单6</div>
<div id="rel">
<div class="pos"></div>
</div>
</div>
</body>
</html>
<script>
$(function(){
//首先给所有对应的菜单标记上顺序
var all = $(".item");
for(var i = 0 ;i<all.length;i++){
var $item = $(all[i]);
$item.attr("data-i",i);
}
//然后监控鼠标滑动到哪一个item上面变化底部的标记
$(".item").mouseenter(function(){
var i = parseInt($(this).attr("data-i"));
$(".pos").stop().animate({
"margin-left":i*100+"px"
},200)
})
})
</script>
思路和拓展
- 反正就是在已有容器结构上再弄一专门展示效果的容器就好了,里面一截多宽多长都能管理好
- 滑动的特效我这里直接animate写上了,当然也可以提前写好动画帧达到更多的动画效果
- 还在纠结昨天的东西,不想用jqueryui来搞,我感觉我的能写出来,但是我忘了一个重要的属性没找到,难受啊
- 碎觉