How to Slide div's off/on page using jQuery

左心房为你撑大大i 提交于 2019-12-06 02:19:30

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);
    }
});

Updated example: http://jsfiddle.net/andrewwhitaker/2uV2h/

The most flexible way would be to add a custom attribute to your DIV tag which has the name of the corresponding box's ID. Eg:

<div class="button" boxid="box1">....

Then in your click function:

$('.button').click(function() {
    mybox=$(this).attr("boxid");
    $(mybox).[whatever code you need to slide the DIV in and out of the screen]
    ....
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!