menu-js.js的文件里有这样一段代码:
当导航菜单.mainlevel下级有div的时候,鼠标在当前区域悬浮和离开时的触发事件
$(function(){ $(".mainlevel:has(div)").hover(function(){ $(this).children("div").stop(true,true).slideDown(300) },function(){ $(this).children("div").stop(true,true).slideUp(1) }) });
index.html中有这样一段代码:
<LI><A href="http://down.admin5.com">高管团队</A><A href="http://down.admin5.com">发展历程</A><A href="http://down.admin5.com">荣誉风采</A> </LI></OL><SPAN class=nav-img><IMG src="images/nav-img-01.jpg"> </SPAN></DIV></LI> <LI id=mainlevel_02 class=mainlevel><A class=nav-a href="http://down.admin5.com">新闻资讯</A> <DIV id=sub_02> <OL> <LI><A href="http://down.admin5.com">通知公告</A><A href="http://down.admin5.com">媒体播报</A> </LI> <LI><A href="http://down.admin5.com">集团新闻</A><A href="http://down.admin5.com">市场信息</A> </LI> <LI><A href="http://down.admin5.com">企业动态</A> </LI></OL><SPAN class=nav-i
W3School上是这样的说明的:
stop(stopAll,goToEnd)
参数 | 描述 |
---|---|
stopAll | 可选。规定是否停止被选元素的所有加入队列的动画。 |
goToEnd | 可选。规定是否允许完成当前的动画。 该参数只能在设置了 stopAll 参数时使用。 |
我的理解:
stopAll 为false时,不停止被选元素所有加入队列的动画,仅停止当前的动画。stopAll为true时,停止所有加入队列的动画(如goToend为true,直接跳到当前动画的最终效果)。
goToend为false时,不允许直接跳到当前动画的最终效果(应该就是所谓的完成当前的动画吧),goToend为true时,允许直接跳到完成当前动画的最终末尾效果
每次运行页面,animater运动时,点击不同button,看到如下不同的效果(animater处在动画1时点击):
点击按钮button1(stop()),由于两个参数都是false。所以点击发生时,animater没有跳到当前动画(动画1)的最终效果,而直接进入动画2,然后动画3,4,5.直至完成整个动画。
点击按钮button1(stop(true)),由于第一个是true,第二个是false,所以animater立刻全部停止了,动画不动了。
点击按钮button1(stop(false,true)),由于第一个是false,第二个是true,所以点击发生时,animater身处的当前动画(动画1)停止并且animater直接跳到当前动画(动画1)的最终末尾效果位置,接着正常执行下面的动画(动画2,3,4,5),直至完成整个动画。
点击按钮button1(stop(true,true)),由于两个都是true,所以点击发生时,animater跳到当前动画(动画1)的最终末尾效果位置,然后,全部动画停止。
工作中遇到过的实际案例:
我在项目里做的一个下拉菜单,当鼠标移上去的时候就菜单显示,当鼠标离开的时候菜单隐藏
true
,
true
),这样每次快速的移入移出菜单,就正常了,当移入一个菜单的时候,停止所有加入队列的动画,但是完成当前的动画(跳至当前动画的最终效果位置)
jQuery多格焦点图效果
* {margin:0; padding:0;}
body {font-size:12px; color:#222; font-family:Verdana,Arial,Helvetica,sans-serif; background:#f0f0f0;}
.clearfix:after {content: “.”; display: block; height: 0; clear: both; visibility: hidden;}
.clearfix {zoom:1;}
ul,li {list-style:none;}
img {border:0;}
.wrapper {width:600px; margin:0 auto; padding-bottom:50px;}
.ad {width:468px; margin:10px auto 0;}
.ad li {padding-top:5px;}
h1 {height:50px; line-height:50px; font-size:22px; font-weight:normal; font-family:”Microsoft YaHei”,SimHei;}
.shuoming {margin-top:20px; border:1px solid #ccc; padding-bottom:10px;}
.shuoming dt {height:30px; line-height:30px; font-weight:bold; text-indent:10px;}
.shuoming dd {line-height:20px; padding:5px 20px;}
.wrapper {width:780px;}
/* tmall focus */
#focus {width:780px; height:380px; overflow:hidden; position:relative;}
#focus ul {height:380px; position:absolute;}
#focus ul li {float:left; width:780px; height:380px; overflow:hidden; position:relative; background:#000;}
#focus ul li div {position:absolute; overflow:hidden;}
#focus .btnBg {position:absolute; width:780px; height:40px; left:0; bottom:0; background:#000;}
#focus .btn {position:absolute; width:770px; height:24px; left:0; bottom:8px; padding-left:10px;}
#focus .btn span {display:inline-block; _display:inline; _zoom:1; width:24px; height:24px; line-height:24px; text-align:center; font-size:20px; font-family:”Microsoft YaHei”,SimHei; margin-right:10px; cursor:pointer; color:#fff;}
#focus .btn span.on {background:#000; color:#fcc;}
$(function() {
var sWidth = $(“#focus”).width(); //获取焦点图的宽度(显示面积)
var len = $(“#focus ul li”).length; //获取焦点图个数
var index = 0;
var picTimer;
//以下代码添加数字按钮和按钮后的半透明长条
var btn = “
for(var i=0; i < len; i++) {
btn += " ” + (i+1) + ““;
}
btn += “
”
$(“#focus”).append(btn);
$(“#focus .btnBg”).css(“opacity”,0.5);
//为数字按钮添加鼠标滑入事件,以显示相应的内容
$(“#focus .btn span”).mouseenter(function() {
index = $(“#focus .btn span”).index(this);
showPics(index);
}).eq(0).trigger(“mouseenter”);
//本例为左右滚动,即所有li元素都是在同一排向左浮动,所以这里需要计算出外围ul元素的宽度
$(“#focus ul”).css(“width”,sWidth * (len + 1));
//鼠标滑入某li中的某div里,调整其同辈div元素的透明度,由于li的背景为黑色,所以会有变暗的效果
$(“#focus ul li div”).hover(function() {
$(this).siblings().css(“opacity”,0.7);
},function() {
$(“#focus ul li div”).css(“opacity”,1);
});
//鼠标滑上焦点图时停止自动播放,滑出时开始自动播放
$(“#focus”).hover(function() {
clearInterval(picTimer);
},function() {
picTimer = setInterval(function() {
if(index == len) { //如果索引值等于li元素个数,说明最后一张图播放完毕,接下来要显示第一张图,即调用showFirPic(),然后将索引值清零
showFirPic();
index = 0;
} else { //如果索引值不等于li元素个数,按普通状态切换,调用showPics()
showPics(index);
}
index++;
},3000); //此3000代表自动播放的间隔,单位:毫秒
}).trigger(“mouseleave”);
//显示图片函数,根据接收的index值显示相应的内容
function showPics(index) { //普通切换
var nowLeft = -index*sWidth; //根据index值计算ul元素的left值
$(“#focus ul”).stop(true,false).animate({“left”:nowLeft},500); //通过animate()调整ul元素滚动到计算出的position
$(“#focus .btn span”).removeClass(“on”).eq(index).addClass(“on”); //为当前的按钮切换到选中的效果
}
function showFirPic() { //最后一张图自动切换到第一张图时专用
$(“#focus ul”).append($(“#focus ul li:first”).clone());
var nowLeft = -len*sWidth; //通过li元素个数计算ul元素的left值,也就是最后一个li元素的右边
$(“#focus ul”).stop(true,false).animate({“left”:nowLeft},500,function() {
//通过callback,在动画结束后把ul元素重新定位到起点,然后删除最后一个复制过去的元素
$(“#focus ul”).css(“left”,”0″);
$(“#focus ul li:last”).remove();
});
$(“#focus .btn span”).removeClass(“on”).eq(0).addClass(“on”); //为第一个按钮添加选中的效果
}
});