简体   繁体   English

使用jQuery的水平div滑动

[英]Horizontal div sliding with jQuery

I've found this little piece of code which fulfils my needs, but is there any way to make "target 1" be loaded defaultly? 我找到了满足我需求的这段小代码,但是有什么方法可以使“ target 1”默认加载? So "right" doesn't start as empty div, but with content from "target 1" already. 因此,“正确”不是以空div开始,而是已经包含“目标1”中的内容。

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

});

http://jsfiddle.net/sg3s/rs2QK/ http://jsfiddle.net/sg3s/rs2QK/

just a little addition 只是一点点

$('a[href=#target1]').click()

Demo 演示版

To have target1 be active be default, you need to give it an active class, and add display: block to it's style in the HTML 要使target1处于活动状态,默认情况下,您需要给它一个active类,并在HTML中添加display: block作为其样式

By not using javascript to set the target1 div to active the page doesn't need to load first before it appears in the right div. 通过不使用javascript将target1 div设置为活动状态,无需在页面显示在正确的div中之前先对其进行加载。 Meaning that if the page is heavy at all it won't flash white because it's waiting for the page to load. 这意味着,如果页面很沉重,它就不会闪烁白色,因为它正在等待页面加载。

<div class="panel active" id="target1" style="background:green; display:block;">Target 1</div>

Like this - http://jsfiddle.net/rs2QK/2291/ 像这样-http://jsfiddle.net/rs2QK/2291/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM