繁体   English   中英

带URL内容的div滑动

[英]sliding div with URL content

我正在尝试创建一个两列页面,其中单击左侧的链接将导致来自另一个URL(在同一域中)的内容水平向右滑动。

这个小提琴是我所追求的一个很好的例子。

我还无法弄清楚如何使用此脚本加载外部内容。 jQuery的.load(url); 似乎是实现此目的的理想方法,但是我无法使其与上面小提琴中显示的jQuery一起使用。

有谁知道这是可以做到的,或者是否有更好的方法来达到同样的效果?

小提琴下面的代码。

HTML:

<div id="left">
    <a href="#target1" class="panel">Target 1</a><br/>
    <a href="#target2" class="panel">Target 2</a><br/>
    <a href="#target3" class="panel">Target 3</a><br/>
</div>

<div id="right">
    <div class="panel" id="target1" style="background:green">Target 1</div>
    <div class="panel" id="target2" style="background:red">Target 2</div>
    <div class="panel" id="target3" style="background:yellow">Target 3</div>
</div>

脚本:

jQuery(function($) {
    $('a.panel').click(function() {
        var $target = $($(this).attr('href')),
            $other = $target.siblings('.active'),
            animIn = function () {
                $target.addClass('active').show().css({
                    left: -($target.width())
                }).animate({
                    left: 0
                }, 500);
            };

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

});

CSS:

#left, #right {
    position: relative;
    float: left;
    margin: 0 5px 0 0;
    border: 1px solid black;
    width: 200px;
    height: 300px;
    overflow: hidden;
}

div.panel {
    position: absolute;
    height: 100%;
    width: 100%;
    display: none;
}

如果您请求的内容不在页面上,那么最好是添加一个服务器端组件(php,ruby等),您可以请求第二部分内容并将服务器发送回去。

$("#target1).click(function(){
$.ajax({
url: "your-script.php", 
data: {target: "send-target-one-please"}, 
success: function(){
//append your second piece of content here
}
}); 
}); 

然后在您的脚本(php)中:

function someFunction(){
//get your content here
$myContent = file_get_contents('../path/to/my/file'); 
echo json_encode($myContent); 
}

显然,这是伪代码,但其基本思想

您可以更改animIn函数以加载内容:

animIn = function () {
    $target.load(URL, function() {
        $target.addClass('active').show().css({
            left: -($target.width())
        }).animate({
            left: 0
        }, 500);
    });
};

只需将URL替换为实际URL。

更新:这是完整的代码

jQuery(function($) {

$('a.panel').click(function() {
    var $target = $($(this).attr('href')),
        $other = $target.siblings('.active'),
        animIn = function () {
            $target.load('/echo/html/', {delay:0.5,html:$target.text()}, function() {
                $target.addClass('active').show().css({
                    left: -($target.width())
                }).animate({
                    left: 0
                }, 500);
            });
        };

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

});

暂无
暂无

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

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