js實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊左上角滑動(dòng)菜單效果代碼
本文實(shí)例講述了js實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊左上角滑動(dòng)菜單效果代碼。分享給大家供大家參考。具體如下:
這里需要鼠標(biāo)點(diǎn)擊激活的網(wǎng)頁左上角菜單,不點(diǎn)擊不會(huì)滑出來,可以改造成二級菜單。
運(yùn)行效果截圖如下:

在線演示地址如下:
http://demo.jb51.net/js/2015/js-click-hd-show-menu-style-codes/
具體代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>左上角導(dǎo)航菜單</title>
<style type="text/css">
#divMenu0 {position:absolute; top:0px; left:30px; visibility:hidden; font-family:arial,helvetica,sans-serif; font-size:18px; font-weight:bold;}
#divMenu1 {position:absolute; top:0px; left:150px; visibility:hidden; font-family:arial,helvetica,sans-serif; font-size:18px; font-weight:bold;}
#divMenu2 {position:absolute; top:0px; left:250px; visibility:hidden; font-family:arial,helvetica,sans-serif; font-size:18px; font-weight:bold;}
</style>
<script language="javascript">
function lib_bwcheck(){ //Browsercheck (needed)
this.ver=navigator.appVersion
this.agent=navigator.userAgent
this.dom=document.getElementById?1:0
this.opera5=this.agent.indexOf("Opera 5")>-1
this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0;
this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
this.ie=this.ie4||this.ie5||this.ie6
this.mac=this.agent.indexOf("Mac")>-1
this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
this.ns4=(document.layers && !this.dom)?1:0;
this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
return this
}
var bw=new lib_bwcheck()
var tMove=10;
var tSpeed=40
var tMoveOnScroll=true
var tShow=20
function makeMenu(obj,nest,show,move,speed){
nest=(!nest) ? "":'document.'+nest+'.'
this.el=bw.dom?document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?eval(nest+'document.'+obj):0;
this.css=bw.dom?document.getElementById(obj).style:bw.ie4?document.all[obj].style:bw.ns4?eval(nest+'document.'+obj):0;
this.x=this.css.left||this.css.pixelLeft||this.el.offsetLeft||0
this.y=this.css.top||this.css.pixelTop||this.el.offsetTop||0
this.state=1; this.go=0; this.mup=b_mup; this.show=show; this.mdown=b_mdown;
this.height=bw.ns4?this.css.document.height:this.el.offsetHeight
this.moveIt=b_moveIt; this.move=move; this.speed=speed
this.obj = obj + "Object"; eval(this.obj + "=this")
}
var px = bw.ns4||window.opera?"":"px";
function b_moveIt(x,y){this.x=x; this.y=y; this.css.left=this.x+px; this.css.top=this.y+px;}
function b_mup(){
if(this.y>-this.height+this.show){
this.go=1; this.moveIt(this.x,this.y-this.move)
setTimeout(this.obj+".mup()",this.speed)
}else{this.go=0; this.state=1}
}
//Menu out
function b_mdown(){
if(this.y<eval(scrolled)){
this.go=1; this.moveIt(this.x,this.y+this.move)
setTimeout(this.obj+".mdown()",this.speed)
}else{this.go=0; this.state=0}
}
function moveTopMenu(num){
if(!oMenu[num].go){
if(!oMenu[num].state)oMenu[num].mup()
else oMenu[num].mdown()
}
for(i=0;i<oMenu.length;i++){
if(i!=num && !oMenu[i].state){ oMenu[i].mup()}
}
}
function checkScrolled(){
for(i=0;i<oMenu.length;i++){
if(!oMenu[i].go){
y=!oMenu[i].state?eval(scrolled):eval(scrolled)-oMenu[i].height+oMenu[i].show
oMenu[i].moveIt(oMenu[i].x,y)
}
}
if(bw.ns4||bw.ns6) setTimeout('checkScrolled()',40)
}
function topMenuInit(){
oMenu=new Array()
oMenu[0]=new makeMenu('divMenu0',"",tShow,tMove,tSpeed)
oMenu[1]=new makeMenu('divMenu1',"",tShow,tMove,tSpeed) //*
oMenu[2]=new makeMenu('divMenu2',"",20,10,20) //*
scrolled=bw.ns4||bw.ns6?"window.pageYOffset":"document.body.scrollTop"
for(i=0;i<oMenu.length;i++){
oMenu[i].moveIt(oMenu[i].x,-oMenu[i].height+oMenu[i].show)
oMenu[i].css.visibility='visible'
}
if(tMoveOnScroll) bw.ns4||bw.ns6?checkScrolled():window.onscroll=checkScrolled;
}
onload=topMenuInit;
</script>
<div id="divMenu0">
<!-- You can just replace this text with some cool images if you want -->
<a href="javascript://">鏈接一</a><br><br>
<a href="javascript://">鏈接二</a><br><br>
<a href="javascript://">鏈接三</a><br><br>
<a href="javascript://">鏈接四</a><br><br>
<a href="#" onclick="moveTopMenu(0); return false">菜單一</a>
</div>
<div id="divMenu1">
<!-- You can just replace this text with some cool images if you want -->
這是第二個(gè)菜單的內(nèi)容<br><br>
<a href="#" onmouseover="moveTopMenu(1)">菜單二</a>
</div>
<div id="divMenu2">
<!-- You can just replace this text with some cool images if you want -->
這是第三個(gè)菜單的內(nèi)容<br>
<a href="#" onclick="moveTopMenu(2); return false">菜單三</a>
</div>
</body>
</html>
希望本文所述對大家的JavaScript程序設(shè)計(jì)有所幫助。
- js實(shí)現(xiàn)鼠標(biāo)拖拽div左右滑動(dòng)
- 基于JavaScript實(shí)現(xiàn)鼠標(biāo)向下滑動(dòng)加載div的代碼
- JS DOM實(shí)現(xiàn)鼠標(biāo)滑動(dòng)圖片效果
- JS將滑動(dòng)門改為選項(xiàng)卡(需鼠標(biāo)點(diǎn)擊)的實(shí)現(xiàn)方法
- javascript鼠標(biāo)滑動(dòng)評分控件完整實(shí)例
- javascript實(shí)現(xiàn)百度地圖鼠標(biāo)滑動(dòng)事件顯示、隱藏
- js實(shí)現(xiàn)鼠標(biāo)感應(yīng)向下滑動(dòng)隱藏菜單的方法
- javascript table美化鼠標(biāo)滑動(dòng)單元格變色
- js自動(dòng)滑動(dòng)+鼠標(biāo)滑動(dòng)區(qū)域
- js實(shí)現(xiàn)鼠標(biāo)滑動(dòng)到某個(gè)div禁止?jié)L動(dòng)
相關(guān)文章
如何在selenium中使用js實(shí)現(xiàn)定位
這篇文章主要介紹了如何在selenium中使用js實(shí)現(xiàn)定位,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Javascript的表單與驗(yàn)證-非空驗(yàn)證
JavaScript 可用來在數(shù)據(jù)被送往服務(wù)器前對 HTML 表單中的這些輸入數(shù)據(jù)進(jìn)行驗(yàn)證。本文給大家介紹javascript的表單與驗(yàn)證-非空驗(yàn)證,對javascript表單驗(yàn)證相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-03-03
二級域名或跨域共享Cookies的實(shí)現(xiàn)方法
適用于Asp。 在主域名設(shè)置的Cookie,在各子域名共用;適用于博客等提供二級域名。這個(gè)問題,以網(wǎng)上有眾多帖子,可惜都沒有完整解決。2008-08-08
詳解關(guān)于微信setData回調(diào)函數(shù)中的坑
這篇文章主要介紹了詳解關(guān)于微信setData回調(diào)函數(shù)中的坑,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
js 獲取html5的data屬性實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猨s 獲取html5的data屬性實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
淺談webpack devtool里的7種SourceMap模式
這篇文章主要介紹了淺談webpack devtool里的7種SourceMap模式,主要介紹了這7種模式的使用和打包編譯后的結(jié)果的不同,非常具有實(shí)用價(jià)值,有興趣的可以了解一下2019-01-01
javascript跨域方法、原理以及出現(xiàn)問題解決方法(詳解)
javascript出于安全方面的考慮,不允許跨域調(diào)用其他頁面的對象。但是在安全限制的同時(shí)也給注入iframe或是ajax應(yīng)用上帶來了不少麻煩。跨域簡單的理解就是因?yàn)閖avascript同源策略的限制,a.com域名下的js無法操作b.com 或者是c.a.com域名下的對象2015-08-08
JavaScript保存并運(yùn)算頁面中數(shù)字類型變量的寫法
這篇文章主要介紹了JavaScript保存并運(yùn)算頁面中數(shù)字類型變量的寫法,當(dāng)你在頁面中需要不停運(yùn)算一個(gè)數(shù)字變量時(shí)非常有用,普通的寫法不能正常運(yùn)算,使用本文方法就可以,需要的朋友可以參考下2015-07-07
ThreeJS從創(chuàng)建場景到使用功能實(shí)例詳解
threejs是一個(gè)用于在瀏覽器中繪制3D圖像的JS庫,它是基于webgl實(shí)現(xiàn)了,包括了webgl1和webgl2的渲染引擎,下面這篇文章主要給大家介紹了關(guān)于ThreeJS從創(chuàng)建場景到使用功能的相關(guān)資料,需要的朋友可以參考下2022-08-08
Javascript Function.prototype.bind詳細(xì)分析
這篇文章主要介紹了Javascript Function.prototype.bind詳細(xì)分析的相關(guān)資料,需要的朋友可以參考下2016-12-12

