如何使用jQuery滑动div的off/on页面 [英] How to Slide div's off/on page using jQuery

查看:87
本文介绍了如何使用jQuery滑动div的off/on页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个函数,该函数可以根据单击哪个按钮/链接来在页面上滑动div.

I'm trying to create a function that slides div's off/on a page depending on which button/link is clicked.

基本结构:

<div class="button"><a href="#">Click Box #1</a> </div>
<div class="button"><a href="#">Click Box #2</a> </div>
<div class="button"><a href="#">Click Box #3</a> </div>

<div id="container">

    <div id="box1" class="box">Box #1</div>
    <div id="box2" class="box">Box #2</div>
    <div id="box3" class="box">Box #3</div>

</div>

当前我正在使用

$('.button').click(function() {
    $('.box').each(function() {

但是,当然,这只不过是一种循环,它只会滑动第一个和最后一个div,我如何才能使每个按钮滑出,然后滑入相应的div.

But of course that only works as a sort of loop and it only slides the first and last div, how can I make each button slides out and then in the appropriate div.

目前为止我所拥有的示例: http://jsfiddle.net/ykbgT/538/

推荐答案

如果您始终要显示第n个框,其中n是您单击的链接的索引,请尝试以下操作:

If you always want to show the nth box, where n is the index of the link you clicked, try something like this:

$('.button').click(function() {
    var index = $(this).index(".button");
    var $box = $(".box:eq(" + index + ")");

    $(".box").not($box).animate({
        left: '150%'
    }, 500);

    if ($box.offset().left < 0) {
        $box.css("left", "150%");
    } else if ($box.offset().left > $('#container').width()) {
        $box.animate({
            left: '50%',
        }, 500);
    }
});

更新的示例: http://jsfiddle.net/andrewwhitaker/2uV2h/

这篇关于如何使用jQuery滑动div的off/on页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆