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

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

问题描述

我正在尝试创建一个函数,该函数根据单击的按钮/链接在页面上滑动 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 的关闭/打开页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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