如何使用 jquery 从左到右或从右到左滑动图像?

发布于 2024-12-10 11:29:52 字数 1311 浏览 0 评论 0原文

我正在尝试为我的网页构建一个简单的图像滑块,这是我使用 jquery、css 编写的代码 -

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="" content="">
<script type="text/javascript" src="jquery.js"></script>
<style type="text/css">
    *{
    margin:0; padding:0;
    }
    #container{
    position:absolute; left:100px; top:100px;
    width:102px;height:102px; 
    border:1px solid red;   
    overflow:hidden; display:inline;
    }
    #container img{float:left;}
</style>
<script type="text/javascript">
    $(document).ready(function(){
        setInterval(slideImages, 5000);
        function slideImages(){
        $('#container img:first-child').clone().appendTo('#container');
        $('#container img:first-child').remove();
        }
    });
</script>
</head>
<body>

    <div id="container">
    <img src="1.jpg" />
    <img src="2.jpg" />
    <img src="3.jpg" />
    <img src="4.jpg" />
    <img src="5.jpg" />
    </div>
</body>
</html>

当代码工作并显示图像时,我想通过以下方式添加更多效果:新图像滑入“容器”窗格。就像从右向左滑动,然后在那里停留一段时间,然后下一张图像再次从右向左滑动来取代现有的图像。

请指导我如何获得从右到左滑动的效果。我知道我可以使用 jquery 向上/向下滑动..但是如何做到 lr 或 rl 呢?

谢谢!

I'm trying to build a simple image slider for my webpage, and here's the code that I have come up with using jquery, css -

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="" content="">
<script type="text/javascript" src="jquery.js"></script>
<style type="text/css">
    *{
    margin:0; padding:0;
    }
    #container{
    position:absolute; left:100px; top:100px;
    width:102px;height:102px; 
    border:1px solid red;   
    overflow:hidden; display:inline;
    }
    #container img{float:left;}
</style>
<script type="text/javascript">
    $(document).ready(function(){
        setInterval(slideImages, 5000);
        function slideImages(){
        $('#container img:first-child').clone().appendTo('#container');
        $('#container img:first-child').remove();
        }
    });
</script>
</head>
<body>

    <div id="container">
    <img src="1.jpg" />
    <img src="2.jpg" />
    <img src="3.jpg" />
    <img src="4.jpg" />
    <img src="5.jpg" />
    </div>
</body>
</html>

While the code works and displays the images, I would like to add some more effects to it by having the new image slide into the 'container' pane. Like sliding from right direction to the left, and then staying there for sometime and then the next image replaces the existin one by again sliding from right to left.

Please guide me as to how to get the sliding r-to-l effect. I know i can slide up/down using jquery.. but how to do it l-r or r-l?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

維他命╮ 2024-12-17 11:29:52

单击此处查看我敲出的示例。您可以轻松更改代码以按时间间隔运行:

JS

$(document).ready(function(){
            $('#rotator > a.arrow.left').click(function(e){
                e.preventDefault;
                var rotator = $('#rotator .images');
                rotator.children('.imageHolder').first().animate({marginLeft:"-=310px"}, function(){
                    $(this).appendTo(rotator).removeAttr("style");
                });
            });
            $('#rotator > a.arrow.right').click(function(e){
                e.preventDefault;
                var rotator = $('#rotator .images');
                rotator.children('.imageHolder').last().prependTo(rotator).removeAttr("style").css("margin-left", "-310px").animate({marginLeft:"0"});
            });
        });

CSS

#rotator{width:310px; height:220px; position:relative; overflow:hidden;  position:relative;}
#rotator .images{width:1000%; position:relative; z-index:1;}
#rotator a.arrow{width:18px; height:41px; display:block; z-index:2; text-indent:-50000em; position:absolute; top:89px; background:#FFF;}
#rotator a.arrow.left{left:0;}
#rotator a.arrow.right{right:0;}
#rotator .images .imageHolder{width:310px; float:left; height:220px; position:relative;}
#rotator  .images .imageHolder span {width:290px; margin: 10px; color:#FFF;  font-size:18px; position:absolute; top:0; left:0; z-index:4;}
#rotator .images .imageHolder img{width:310px;  height:220px; position:absolute; display:block; top:0; left:0; z-index:3;}

HTML

<!DOCTYPE html>
<html>

<body>
    <div id="rotator">
        <a href="#" class="arrow left"></a>
        <div class="images">
            <div class="imageHolder">
                <span>Image Number 1</span>
                <img src="http://www.random-images.com/image/obj21geo38pg1p5.jpg" alt="" />
            </div>
            <div class="imageHolder">
                <span>Image Number 2</span>
                <img src="http://www.jpl.nasa.gov/images/wise/20100217/pia12832-browse.jpg" alt="" />
            </div>
            <div class="imageHolder">
                <span>Image Number 3</span>
                <img src="http://www.boingboing.net/images/_images__wikipedia_commons_a_aa_Polarlicht_2.jpg" alt="" />
            </div>
            <div class="imageHolder">
                <span>Image Number 4</span>
                <img src="http://www.aviation-images.com/user/zooms/118/451nw/aviation-images.com21079968wm.jpg?d=1179938582" alt="" />
            </div>
        </div>
        <a href="#" class="arrow right"></a>
    </div>
</body>
</html>

Click here to view example I knocked up. You can easily change the code to work on a timed interval:

JS

$(document).ready(function(){
            $('#rotator > a.arrow.left').click(function(e){
                e.preventDefault;
                var rotator = $('#rotator .images');
                rotator.children('.imageHolder').first().animate({marginLeft:"-=310px"}, function(){
                    $(this).appendTo(rotator).removeAttr("style");
                });
            });
            $('#rotator > a.arrow.right').click(function(e){
                e.preventDefault;
                var rotator = $('#rotator .images');
                rotator.children('.imageHolder').last().prependTo(rotator).removeAttr("style").css("margin-left", "-310px").animate({marginLeft:"0"});
            });
        });

CSS

#rotator{width:310px; height:220px; position:relative; overflow:hidden;  position:relative;}
#rotator .images{width:1000%; position:relative; z-index:1;}
#rotator a.arrow{width:18px; height:41px; display:block; z-index:2; text-indent:-50000em; position:absolute; top:89px; background:#FFF;}
#rotator a.arrow.left{left:0;}
#rotator a.arrow.right{right:0;}
#rotator .images .imageHolder{width:310px; float:left; height:220px; position:relative;}
#rotator  .images .imageHolder span {width:290px; margin: 10px; color:#FFF;  font-size:18px; position:absolute; top:0; left:0; z-index:4;}
#rotator .images .imageHolder img{width:310px;  height:220px; position:absolute; display:block; top:0; left:0; z-index:3;}

HTML

<!DOCTYPE html>
<html>

<body>
    <div id="rotator">
        <a href="#" class="arrow left"></a>
        <div class="images">
            <div class="imageHolder">
                <span>Image Number 1</span>
                <img src="http://www.random-images.com/image/obj21geo38pg1p5.jpg" alt="" />
            </div>
            <div class="imageHolder">
                <span>Image Number 2</span>
                <img src="http://www.jpl.nasa.gov/images/wise/20100217/pia12832-browse.jpg" alt="" />
            </div>
            <div class="imageHolder">
                <span>Image Number 3</span>
                <img src="http://www.boingboing.net/images/_images__wikipedia_commons_a_aa_Polarlicht_2.jpg" alt="" />
            </div>
            <div class="imageHolder">
                <span>Image Number 4</span>
                <img src="http://www.aviation-images.com/user/zooms/118/451nw/aviation-images.com21079968wm.jpg?d=1179938582" alt="" />
            </div>
        </div>
        <a href="#" class="arrow right"></a>
    </div>
</body>
</html>
泪意 2024-12-17 11:29:52

Slide 适合您吗?

$("div").click(function () {
      $(this).hide("slide", { direction: "left" }, 1000);
});

Will Slide works for you?

$("div").click(function () {
      $(this).hide("slide", { direction: "left" }, 1000);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文