如何水平移动div

时间:2013-10-03 05:59:19

标签: javascript jquery html css

我正在尝试div框,图像从左向右移动,

我尝试了另一个脚本:http://jsfiddle.net/bala2024/MzHmN/

这是html代码

<!DOCTYPE html>
<html>    
    <head></head>    
    <body style>
        <div id="slideleft" class="slide">
            <button>slide it</button>
            <div class="inner">Slide to the left</div>
            <div style="width:50px; height:50px">
                <img src="sceen.jpg">
            </div>
        </div>
    </body>

</html>

CSS

.slide {
    position: relative;
    background-color: gray;
    height: 100px;
    width: 500px;
    overflow: hidden;
}
.slide .inner {
    position: absolute;
    left: -500px;
    bottom: 0;
    background-color:#e3e3e3;
    height: 30px;
    width: 500px;
}

JS

 $(document).ready(function () {
     $('#slideleft button').click(function () {
         var $lefty = $(this).next();
         $lefty.animate({
             left: parseInt($lefty.css('left'), 10) == 0 ? -$lefty.outerWidth() : 0
         });
     });
 });

4 个答案:

答案 0 :(得分:1)

您需要将宽度应用于主容器。请将其替换为以下行,并检查您的浏览器。

 <div id="slideleft" class="slide" style="width:100px; margin:0 auto">

答案 1 :(得分:0)

使用外边距边距和内部空间填充 试试这个

<!DOCTYPE html>
<html>
<head>  
</head>
<body style>
    <div id="slideleft" class="slide">
        <button>slide it</button>
        <div class="inner">Slide to the left</div>
        <div style="width:50px; height:50px; margin-left: 100px;"><img src="sceen.jpg">
       </div>         
    </div>   
</body>
</html>

答案 2 :(得分:0)

点击以下链接观看直播演示...... Click Here

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Animation test</title>
<style>

</style>
<script type="text/javascript">
$(document).ready(function() {
    $("#b").animate({left: "+=500"}, 2000);
    $("#b").animate({left: "-=300"}, 1000);
});

</script>
</head>

<body>
<div style="position:relative">
    <div id="b" style="position:absolute;">B</div>
</div>
</body>
</html>

答案 3 :(得分:-1)

CSS:

div.inner{
background:black;
margin-left:0;
width:100px;
animation:sample 2s;
-webkit-animation:sample 2s;
}
keyframes sample{
from{margin-left:100%;}
to{margin-left:0%;}
}
@-webkit-keyframes sample{
from{margin-left:100%;}
to{margin-left:0%;}
}

小提琴:http://jsfiddle.net/apRMU/17/