<!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">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery动画特效</title>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" type="text/javascript"></script>
<style>
span{
position:absolute;
width:40px;
height:40px;
border: solid 1px #ccc;
margin: 0px 8px;
background-color: red;
color:white;
vertical-align:middle;
left: 18px;
top: 15px;
}
div
{
margin: 10px 0px;
position:relative;
}
#btnStop{
position: absolute;
top: 25%;
}
#close{
position: absolute;
top: 30%;
}
.bazi{
width: 90%;
height: 40px;
background-color: blue;
margin: 0 auto;
margin-top: 5%;
}
.
</style>
</head>
<body>
<input id="btnStop" type="button" value="停止" />
<input id="close" type="button" value="再来一次" />
<div class="bazi"></div>
<span></span>
<div id="tip"></div>
<script>
aim();
function aim(){
$("span").animate({
left:"+=72%"
}, 3000, function () {
$(this).animate({
left:"-=72%"
}, 3000, function () {
});
});
$("#btnStop").bind("click", function () {
if($("span").css("left","40%")){
$("#tip").html(parseInt(Math.random()*(1000-700+1) + 700));
}else{
$("#tip").html(600);
}
$("span").stop();
clearInterval(i);
});
};
var i=setInterval(function(){
aim();
},6000);
/* $("#close").bind("click", function () {
$("span").css("left","18px");
aim();
var j=setInterval(function(){
aim();
},6000);
$("#btnStop").bind("click", function () {
$("span").stop();
clearInterval(j);
$("#tip").html("执行停止!");
});
});*/
</script>
</body>
</html>
请问是我的判断语句没写好吗,,无论移动在哪个位置都只停在中间
我只是希望它停在中间的时候金额是随机的,在两边金额是固定600
$("#btnStop").bind("click", function () {
// if($("span").css("left","40%")){ 你这个是赋值,返回true的。
var $span = $('span');
if($span.offset().left / document.body.offsetWidth < 0.4){
$("#tip").html(parseInt(Math.random()*(1000-700+1) + 700));
}else{
$("#tip").html(600);
}
$("span").stop();
clearInterval(i);
});
};
确实是这样,,只是我想问下,,这个好像判断条件不对,,,如何让位置left=45%的时候,,,值是随机的呢,,然后两边的数值都固定是600
@像风一样自由~~~: 那你把< 符号修改为等于哇,而且需要注意下,js浮点数运算的精度问题。