0

我需要让一个彩色的 div 水平向右移动,当它碰到边缘时,它的大小应该是两倍,并且围绕中心旋转的速度是两倍。

var topPosition = 50;
var leftPosition = 250;
var rightPosition = 800;
  function move(){
  var go = document.getElementById("box");
  go.style.left = leftPosition + "px";  
  go.style.right = rightPosition + "px";
  go.style.visibility = "visible";
  ++leftPosition;

  if (leftPosition == 800){
--leftPosition;

它像我告诉它的那样停在 800 像素,但它不会回去

4

3 回答 3

2

让我们稍微清理一下代码并实现您想要的。为了:

  1. 移动到 800 像素
  2. 当 1 完成后,返回,速度加倍,大小加倍。

我们将使用一个作用域变量来做到这一点:speed. speed将是默认的速度和方向。

setInterval为了不阻止页面的执行,我还分离了您的代码。

function move() {
    var elem = document.getElementById("box"),
        speed = 1,
        currentPos = 0;
    // Reset the element
    elem.style.left = 0+"px";
    elem.style.right = "auto";
    var motionInterval = setInterval(function() {
        currentPos += speed;
        if (currentPos >= 800 && speed > 0) {
           currentPos = 800;
           speed = -2 * speed;
           elem.style.width = parseInt(elem.style.width)*2+"px";
           elem.style.height = parseInt(elem.style.height)*2+"px";
        }
        if (currentPos <= 0 && speed < 0) {
           clearInterval(motionInterval);
        }
        elem.style.left = currentPos+"px";
    },20);
}

小提琴:http ://tinker.io/7d393 。你会看到,它有效。

于 2013-05-22T18:55:23.777 回答
0

这是一个(主要是)CSS解决方案: http: //tinker.io/7d393/6

HTML:

<div id="box"></div>

<div style="position: absolute; top: 200px"><button>Move it</button></div>

CSS:

#box {
    background: blue;
    position: absolute;
    width:100px;
    height: 100px;
}
#box.move{
    -webkit-animation: myanim 5s;
    animation: myanim 5s;
}
@-webkit-keyframes myanim
{
0%   {top:0;left:0; width: 100px; height: 100px;}
66%  {top:0;left:800px; width:100px;height:100px;}
67%  {top:0;left:800px; width:200px; height: 200px;}
100% {top:0; left:0; width: 200px; height:200px;}
}
@keyframes myanim
{
0%   {top:0;left:0; width: 100px; height: 100px;}
66%  {top:0;left:800px; width:100px;height:100px;}
67%  {top:0;left:800px; width:200px; height: 200px;}
100% {top:0; left:0; width: 200px; height:200px;}
}

JS:

//jQuery as I do not know what nav you are using.
$(function() {
    $("button").click(function(){ 
        $('#box').toggleClass('move')});
});

这是一个主要是 jQuery 的解决方案: http: //tinker.io/7d393/7

HTML:

<div id="box"></div>

<div style="position: absolute; top: 200px"><button>Move it</button></div>

CSS:

#box {
    background: blue;
    position: absolute;
    width:100px;
    height: 100px;
}

JS:

//jQuery as I do not know what nav you are using.
$(function() {
    $("button").click(function(){
        $('#box')
            .css({left:0,top:0,width:'100px',height:'100px'})
            .animate({left:'800px',top:0,width:'100px',height:'100px'},3000)
            .animate({left:'800px',top:0,width:'200px',height:'200px'},20)
            .animate({left:0,top:0,width:'200px',height:'200px'},1500);
    });
});
于 2013-05-22T23:45:59.427 回答
0

想想一旦leftPosition达到 799 会发生什么:

  ++leftPosition;                #leftPostion == 800

  if (leftPosition == 800){      #This becomes true
    --leftPosition;              #leftPostion == 799

所以你从你离开的地方开始,这将在你下次打电话时重复move()

要解决此问题,您需要为运动添加方向:

var topPosition = 50;
var leftPosition = 250;
var rightPosition = 800;
var direction = 1;     
function move(){
  var go = document.getElementById("box");
  go.style.left = leftPosition + "px";  
  go.style.right = rightPosition + "px";
  go.style.visibility = "visible";
  leftPosition += direction;

  if (leftPosition == 800){
    direction = -1;
于 2013-05-22T18:47:19.873 回答