这篇文章将为大家详细讲解有关使用jQuery怎么实现一个老虎机跑动效果,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
jquery是什么
jquery是一个简洁而快速的JavaScript库,它具有独特的链式语法和短小清晰的多功能接口、高效灵活的css选择器,并且可对CSS选择器进行扩展、拥有便捷的插件扩展机制和丰富的插件,是继Prototype之后又一个优秀的JavaScript代码库,能够用于简化事件处理、HTML文档遍历、Ajax交互和动画,以便快速开发网站。
注意需要自行引用jquery库
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>老虎机</title>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<style type="text/css">
#bigDiv div{
height:50px;
width:50px;
float:left;
background-color:red;
margin-left:5px;
visibility: hidden;
}
#bigDiv p{
margin-left: 10px;
}
</style>
</head>
<body>
<div align="center">
<div id = bigDiv>
<div><p>西瓜</p></div>
<div><p>苹果</p></div>
<div><p>香蕉</p></div>
<div><p>橘子</p></div>
<div><p>梨子</p></div>
<div><p>哈密瓜</p></div>
<div><p>草莓</p></div>
<div><p>水蜜桃</p></div>
<div><p>橙子</p></div>
<div><p>谢谢</p></div>
</div>
<br/><br/><br/><br/>
<input type="button" id="autoBtn" value="Auto" onclick="autoStop()">
</div>
<script language="javascript">
var allDiv = $("#bigDiv").find("div");
var t;
var tCicrl = 30;
var c = 0;
function autoStop(){
var index = 11;//11取值范围是大于已有选项项数
$(allDiv).each(function(i){
if($(this).css("visibility")!="hidden"){
index = i;
}
});
if(index == 11){
index = parseInt(9*Math.random());
}
$(allDiv).eq(index).css("visibility","visible");
setTimeout(function(){slotRun(index);},50);
}
function slotRun(index){
if(c<150){
if($(allDiv).eq(index).css("visibility")!="hidden")
{
$(allDiv).eq(index).css("visibility","hidden");
if(index==9){
//clearTimeout(t);
$(allDiv).eq(0).css("visibility","visible");
t = setTimeout(function(){slotRun(0)},tCicrl++);
}else{
//clearTimeout(t);
$(allDiv).eq(index+1).css("visibility","visible");
t = setTimeout(function(){slotRun(++index)},tCicrl++);
}
c++;
}
}else{
clearTimeout(t);
tCicrl = 30;
c = 0;
}
}
</script>
</body>
</html>
关于使用jQuery怎么实现一个老虎机跑动效果就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。