1

我想从左到右滑动或移动图像,例如

http://rajeevkumarsingh.wix.com/pramtechnology

移动的读取五边形框 Ok!

我尝试了一下,但没有这样做。我使用的代码如下:

<script type="text/javascript">

<!--
var imgObj = null;
var animate ;
function init(){
   imgObj = document.getElementById('myImage');
   imgObj.style.position= 'absolute'; 
   imgObj.style.top = '240px';
   imgObj.style.left = '-300px';
   imgObj.style.visibility='hidden';
   moveRight();
} 
function moveRight(){
if (parseInt(imgObj.style.left)<=10)
{
   imgObj.style.left = parseInt(imgObj.style.left) + 5 + 'px';
   imgObj.style.visibility='visible';
   animate = setTimeout(moveRight,20); // call moveRight in 20msec
   //stopanimate = setTimeout(moveRight,20);
  }
else
  stop();
  f();
}

function stop(){
   clearTimeout(animate);
}
window.onload =init;
//-->
</script>
<img id="myImage" src="xyz.gif" style="margin-left:170px;" />

Firefox 和 IE 也存在某种分辨率问题。如何解决它们。我也无法如此清楚地移动这些东西。这可能吗?我希望它使用 JavaScript 而不是 Flash。

4

3 回答 3

7
var animate, left=0, imgObj=null;

function init(){

   imgObj = document.getElementById('myImage');
   imgObj.style.position= 'absolute';
   imgObj.style.top = '240px';
   imgObj.style.left = '-300px';
   imgObj.style.visibility='hidden';

   moveRight();
}

function moveRight(){
    left = parseInt(imgObj.style.left, 10);

    if (10 >= left) {
        imgObj.style.left = (left + 5) + 'px';
        imgObj.style.visibility='visible';

        animate = setTimeout(function(){moveRight();},20); // call moveRight in 20msec

        //stopanimate = setTimeout(moveRight,20);
    } else {
        stop();
    }
    //f();
}

function stop(){
   clearTimeout(animate);
}

window.onload = function() {init();};
于 2012-07-12T21:36:10.473 回答
3

这是一个将图像向右移动以及从 .js 文件而不是直接从索引页面淡入的示例:

----------我的index.html页面----------

<!DOCTYPE html>
<html>
<body>

<script src="myJava.js"></script>

<script language="javascript" type="text/javascript">
    //In pixels
    var amountToMoveTotal = 1000;
    var amountToMovePerStep = 10;
    //In milliseconds
    var timeToWaitBeforeNextIncrement = 20;

    var amountToFadeTotal = 1.0;
    var amountToFadePerStep = 0.01;
</script>

<p>This one moves right on hover</p>
<img onmouseover="moveRight(this, amountToMoveTotal, amountToMovePerStep, timeToWaitBeforeNextIncrement)" src="http://www.w3schools.com/jsref/smiley.gif" style="left:0px;top:50px;position:absolute;opacity:1.0;">
<br><br>
<p>This one fades in on hover</p>
<img onmouseover="fadeIn(this, amountToFadeTotal, amountToFadePerStep, timeToWaitBeforeNextIncrement)" src="http://www.w3schools.com/jsref/smiley.gif" style="left:0px;top:150px;position:absolute;opacity:0.1;">

</body>
</html>

----------我的myJava.js代码----------

var animate;
var original = null;

function moveRight(imgObj, amountToMoveTotal, amountToMovePerStep, timeToWaitBeforeNextIncrement)
{
    //Copy original image location
    if (original === null){
        original = parseInt(imgObj.style.left);
    }

    //Check if the image has moved the distance requested
    //If the image has not moved requested distance continue moving.
    if (parseInt(imgObj.style.left) < amountToMoveTotal) {
        imgObj.style.left = (parseInt(imgObj.style.left) + amountToMovePerStep) + 'px';

        animate = setTimeout(function(){moveRight(imgObj, amountToMoveTotal, amountToMovePerStep, timeToWaitBeforeNextIncrement);},timeToWaitBeforeNextIncrement);
    }else {
        imgObj.style.left = original;
        original = null;        
        clearTimeout(animate);
    }
}
function fadeIn(imgObj, amountToFadeTotal, amountToFadePerStep, timeToWaitBeforeNextIncrement)
{
    //Copy original opacity
    if (original === null){
        original = parseFloat(imgObj.style.opacity);
    }

    //Check if the image has faded the amount requested
    //If the image has not faded requested amount continue fading.
    if (parseFloat(imgObj.style.opacity) < amountToFadeTotal) {
        imgObj.style.opacity = (parseFloat(imgObj.style.opacity) + amountToFadePerStep);

        animate = setTimeout(function(){fadeIn(imgObj, amountToFadeTotal, amountToFadePerStep, timeToWaitBeforeNextIncrement);},timeToWaitBeforeNextIncrement);
    }else {
        imgObj.style.opacity = original;
        original = null;
        clearTimeout(animate);
    }
}
于 2014-03-28T08:01:23.907 回答
1

使用jQuery库,真的很容易做你需要的

http://api.jquery.com/animate/

按照页面示例代码:http: //api.jquery.com/stop/

<!DOCTYPE html>
<html>
<head>
  <style>div { 
position: absolute; 
background-color: #abc;
left: 0px;
top:30px;
width: 60px; 
height: 60px;
margin: 5px; 
}
</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <button id="go">Go</button> 
<button id="stop">STOP!</button>
<button id="back">Back</button>
<div class="block"></div>
<script>
/* Start animation */
$("#go").click(function(){
$(".block").animate({left: '+=100px'}, 2000);
});

/* Stop animation when button is clicked */
$("#stop").click(function(){
$(".block").stop();
});

/* Start animation in the opposite direction */
$("#back").click(function(){
$(".block").animate({left: '-=100px'}, 2000);
});

</script>

</body>
</html>
于 2012-07-12T21:12:24.607 回答