基于style.right重置位置
Resetting position based on style.right
当图像到达屏幕的最右边时,我试图重置动画。
没有这条线,一切都很好:
if (imgObj.style.right == 0) {
Reset();
}
这不是正确的方式吗?
<!--
var imgObj = null;
var animate;
function init() {
imgObj = document.getElementById('myImage');
imgObj.style.position = 'relative';
imgObj.style.left = '0px';
}
function moveRight() {
imgObj.style.left = parseInt(imgObj.style.left) + 10 + 'px';
animate = setTimeout(moveRight, 2000); // call moveRight in 20msec
if (imgObj.style.right == 0) {
Reset();
}
}
function Reset() {
clearTimeout(animate);
imgObj.style.left = '0px';
}
window.onload = init;
//-->
<form>
<img id="myImage" src="http://jsfiddle.net/img/logo.png" />
<p>Click the buttons below to handle animation</p>
<input type="button" value="Start" onclick="moveRight();" />
<input type="button" value="Reset" onclick="reset();" />
</form>
如何使动画与上一行一起工作?
您有一些错误
-
onclick="Reset();"
而非onclick="reset();"
-
parseInt(imgObj.style.left.replace("px",""))
而非parseInt(imgObj.style.left)
-
imgObj.style.right
总是",你必须设置一些类似imgObj.style.left.replace("px","")>200
的东西
var imgObj = null;
var animate;
function init() {
imgObj = document.getElementById('myImage');
imgObj.style.position = 'relative';
imgObj.style.left = '0px';
}
function moveRight() {
imgObj.style.left = parseInt(imgObj.style.left.replace("px","")) + 10 + 'px';
animate = setTimeout(moveRight, 2000); // call moveRight in 20msec
if (parseInt(imgObj.style.left.replace("px",""))+ imgObj.width>= window.innerWidth) {
Reset();
}
}
function Reset(){
clearTimeout(animate);
imgObj.style.left = '0px';
}
window.onload = init;
<form>
<img id="myImage" src="http://jsfiddle.net/img/logo.png" />
<p>Click the buttons below to handle animation</p>
<input type="button" value="Start" onclick="moveRight();" />
<input type="button" value="Reset" onclick="Reset();return false;" />
</form>
相关文章:
- 如何在玩TimelineMax(GSAP)后重置原始位置
- 如何重置HTMLAudioElement's的当前播放位置为0
- 当滚动到指定的位置时,启动功能,如何重置它并重新启动,如果我们再次回到这个位置
- easeljs在不同的位置多次添加位图到舞台上
- 停止动画并重置拉斐尔.js中元素的位置
- 如何在到达帧末尾时将其位置重置回原点
- 同时加载 iframe 并重置文档位置
- 如何防止轮播容器在重置位置时过渡
- 使用轨迹球控制时重置摄像机位置/旋转
- 在拖动事件中重置位置
- 重置 jQuery UI 滑块控点位置
- 停止浮动标签重置回原始位置
- 在没有数组的4位数字中交换数字的位置
- 如何进行碰撞预防?(重置玩家对象的位置)
- 基于style.right重置位置
- 反弹球会重置到所需位置,但不会移动
- 每次单独的动画到达某个位置时,运行并重置一个动画
- 如何:使用滚动条重置分割内的滚动条位置
- 美元(窗口).调整大小重置滚动位置
- 防止“向上箭头”;键在文本框内重置光标位置