jQuery 水平切换 Div

标签 jquery

我正在尝试创建一个滑出 div 功能。基本上,它是一个矩形图像 map - 当您单击 map 上的某个位置时,一个 DIV 从右侧滑出以覆盖整个 map 。我需要对多个位置(即多个 DIV)执行此操作

我尝试了 jQuery 的一些函数,但没有成功。切换效果仅允许向上或向下运动,并且我无法让 .animate 效果按我想要的方式工作。

这个Jfiddle接近我需要的,但我试图让事件的 DIV 在新的 DIV 滑入之前滑出(并且 MAP DIV 应始终保持静态,因此当 DIV 滑出时,您会在新的 div 之前看到 map 滑入)。

这是 Jfiddle 中的内容:

jQuery(function($) {

    $('a.panel').click(function() {
        var $target = $($(this).attr('href')),
            $other = $target.siblings('.active');

        if (!$target.hasClass('active')) {
            $other.each(function(index, self) {
                var $this = $(this);
                $this.removeClass('active').animate({
                    left: $this.width()
                }, 500);
            });

            $target.addClass('active').show().css({
                left: -($target.width())
            }).animate({
                left: 0
            }, 500);
        }
    });
});​

最佳答案

根据你的 jQuery 片段,我想出了

$(document).ready(function() {
    $('div.panel').css('left', '-200px');

    $('a.panel').click(function() {
        var a = $(this);
        var p = $(a.attr('href'));
        var pw = p.width();

        //-- if we have an active panel, hide it. otherwise, just show target
        if ($('div.panel').hasClass('active')) {
            //-- reset all panels
            $('div.panel').animate({
                left: -pw
            }, 500, function() {
                //-- when that is complete, move target panel to position
                p.addClass('active').show().animate({
                    left: 0
                });
            });
        } else {
            p.addClass('active').show().animate({
                left: 0
            });
        }
    });
});

更新了 jsFiddle: http://jsfiddle.net/rs2QK/982/

  • 更新#1 CSS/HTML 保持原始 map div (#target1) 显示。 CSS 有点多余,但考虑到这种方法,它可以完成工作。

  • 更新 #2 更新了 fiddle 链接。我的印象是它在保存时更新了。哎呀。

关于jQuery 水平切换 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13475004/

相关文章:

iphone - 使用 JQuery 和 Scrollto 插件自动滚动时 iPad 闪烁

javascript - 当前年份的 jQuery 日期选择器 - 10

javascript - 如何快速从 DOM 中临时删除元素并按需返回它们?

javascript - 在文本大于的 span 之后添加 HTML

jquery - 在jquery中创建嵌套列表

javascript - 灯箱中的图像顺序

javascript - Twitter Bootstrap - 使用一组轮播指示器控制多个轮播

javascript - 滚动到 ng-repeat 中数组中最后添加的记录

javascript - 关于 jquery .text 方法,有什么我应该知道的吗?

javascript - 如何在悬停时指定图像的位置