JS實現(xiàn)常用導(dǎo)航鼠標(biāo)下經(jīng)過下方橫線自動跟隨效果
js寫常用導(dǎo)航鼠標(biāo)下經(jīng)過下方橫線自動跟隨

html代碼如下:
<div class="header">
<ul class="nav fr">
<li class="nav-item nav-line">
<a href="/" class="nav-link">首頁</a>
</li>
<li class="nav-item nav-line">
<a href="/" class="nav-link">公司介紹</a>
</li>
<li class="nav-item nav-line">
<a href="/" class="nav-link">產(chǎn)品及解決方案</a>
</li>
<li class="nav-item nav-line">
<a href="/" class="nav-link">客戶案例</a>
</li>
<li class="nav-item nav-line">
<a href="/" class="nav-link">聯(lián)系我們</a>
</li>
</ul>
<div class="wrap-line" style="opacity:0"></div>
</div>css代碼如下:
.header{position: absolute; height: 60px; top: 0; left: 0; right: 0; color: #fff;background: rgba(0,0,0,.3); }
.header ul{ padding: 0; margin: 0;}
.header .nav-item{ padding: 0 20px; display: inline-block;}
.header .nav-item a{ text-decoration: none;}
.header .nav-item .nav-link, .header .nav-item:hover .nav-link {color: #fff;}
.header .nav-item .nav-link{ padding: 0; font-size: 15px; height: 60px; line-height: 60px;}
.wrap-line{ display: block; position: absolute; height: 3px; bottom: 5px; background: #fff;}
js代碼如下:
<script src="jquery.min.js"></script>
<script>
// 導(dǎo)航滑動效果
$(function () {
$('.nav .nav-line').hover(function() {
$('.wrap-line').stop().animate({
left: $(this).position().left + 25,
width: $(this).outerWidth() - 50,
opacity: 1
});
}, function() {
$('.wrap-line').stop().animate({
opacity: 0
});
});
})
</script>PS:css + js 實現(xiàn)導(dǎo)航欄下劃線跟隨鼠標(biāo)滑動效果
一個vue導(dǎo)航欄下劃線跟隨鼠標(biāo)滑動的效果,純 js + css

滑動效果
下劃線會跟隨這鼠標(biāo)滑動,并且激活的item下劃線會消失
最終代碼
<div class="cc">
<div class="aa_box" ref="aa" @mouseleave="handleMouseLeave">
<div
class="aa_item"
v-for="(i, index) in navList"
:key="i"
@click="aaBtn(index)"
@mouseenter="handleMouseEnter(index)"
:class="{ active: index == activeIndex || moveActiveIndex == index }"
>
{{ i }}
</div>
</div>
<div class="aa-line" :style="{ left: handleX + 'px' }"></div>
</div> data() {
return {
activeIndex: 0,
moveActiveIndex: null,
X: 0,
current: 0,
aa_x: 0,
mouse_x: 0,
isMove: false
};
},
computed: {
handleX() {
return this.isMove ? this.mouse_x : this.aa_x;
}
},
methods: {
aaBtn(index) {
this.activeIndex = index;
this.aa_x = this.handleMouver(index);
},
handleMouseEnter(index) {
this.isMove = true;
this.moveActiveIndex = index;
this.mouse_x = this.handleMouver(index);
},
handleMouseLeave() {
this.isMove = false;
this.mouse_x = 0;
this.moveActiveIndex = null;
},
handleMouver(index) {
const aa = this.$refs["aa"].children;
let num = 0;
for (let i = 0; i < aa.length; i++) {
const item = aa[i];
if (i + 1 <= index) {
const itemWidth = item.clientWidth + 50;
num += itemWidth;
}
}
return num;
}
},
到此這篇關(guān)于JS寫常用導(dǎo)航鼠標(biāo)下經(jīng)過下方橫線自動跟隨的文章就介紹到這了,更多相關(guān)js導(dǎo)航鼠標(biāo)自動跟隨內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
TypeScript創(chuàng)建一個簡單Web應(yīng)用
這篇文章主要為大家介紹了TypeScript創(chuàng)建一個簡單Web應(yīng)用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
javaScript實現(xiàn)復(fù)選框全選反選事件詳解
這篇文章主要為大家詳細(xì)介紹了javaScript實現(xiàn)復(fù)選框全選反選事件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09
JS排序算法之希爾排序與快速排序?qū)崿F(xiàn)方法
這篇文章主要介紹了JS排序算法之希爾排序與快速排序?qū)崿F(xiàn)方法,結(jié)合實例形式分析了希爾排序與快速排序的原理及javascript實現(xiàn)技巧,需要的朋友可以參考下2017-12-12
微信小程序開發(fā)的四十個技術(shù)竅門總結(jié)(推薦)
這篇文章主要給大家介紹了微信小程序開發(fā)的四十個技術(shù)竅門的相關(guān)資料,相信對大家的學(xué)習(xí)或者使用微信小程序具有一定的參考借鑒價值,所以特別推薦給大家,需要的朋友們可以一起來看看吧。2017-01-01

