让div在屏幕上移动

时间:2013-05-22 18:41:58

标签: javascript html5

我需要将彩色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像素,就像我告诉它但不会回去

3 个答案:

答案 0 :(得分:2)

让我们清理一下代码并实现你想要的。按顺序:

  1. 移至800px
  2. 当1完成时,返回,快两倍,并加倍。
  3. 我们将使用一个范围变量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。你会看到,它有效。

答案 1 :(得分: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;

答案 2 :(得分: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);
    });
});