setInterval() does not work on style.marginLeft
setInterval() does not work on style.marginLeft
我希望我创建的盒子每0.5秒移动20px,但setInterval()
只发生一次
<style type="text/css">
#box{
background-color: green;
width: 200px;
height: 200px;
}
</style>
</head>
<body>
<div id="box"></div>
<script type="text/javascript">
setInterval(function(){
push();
},500);
function push(){
var box = document.getElementById('box');
box.style.marginLeft="20px";
}
</script>
每次迭代都需要添加20个额外的像素
setInterval(function(){
push();
},500);
function push(){
var box = document.getElementById('box');
box.style.marginLeft= parseInt(box.style.marginLeft||0) + 20 + "px";
}
#box{
background-color: green;
width: 200px;
height: 200px;
}
<div id="box"></div>
推送功能需要修改
<script type="text/javascript">
setInterval(function(){
push();
},500);
function push(){
var box = document.getElementById('box'),
margin=parseFloat(box.style.marginLeft);
box.style.marginLeft=(margin+20)+"px";
}
</script>
相关文章:
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- I'我设计了一个下拉菜单,onclick会出现,而on blur会消失
- window.on.scroll事件未启动
- Socket.io on Mozilla Rhino
- CSS中的游标属性似乎不适用于USB On The Go
- jQuery点击ON现在使用.load触发关闭
- JQuery.on(“keydown”)追加到页面时不工作
- 使用.on动态添加jquery/js不知道的html元素
- angularjs$valid-on-dates在firefox中报告错误
- addEventListener on NodeList
- 当我在节点上拖动鼠标时,我如何防止使用d3.ehavior.zoom().on(“缩放”,重绘)
- jQuery 1.7.2:.on()在一个页面上工作;Don’别再干别的了
- Jquery on单击“显示信息”
- 平均值.on(事件[,选择器][,数据],处理程序)
- 多个jQuery.on()事件无法运行xBrowser(取决于顺序)
- 如何使jQuery的“bind”或“on”事件处理程序幂等
- document.getElementById.style.backgroundImage not working
- 无法使以下ng on click outside指令在同一页面上的两个元素上工作
- Javascript mozfullscreenchange on Firefox
- setInterval() does not work on style.marginLeft