jQuery我如何.slideUp()当前的图像

时间:2015-11-09 02:36:13

标签: javascript jquery

我在尝试.slideUp()当前图片方面遇到了一些麻烦。它只是.slideUp()我点击的链接后面的图像。我不确定如何获取当前图像的路径,而不是将图像向上滑动。

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Image Gallery</title>
    <link rel="stylesheet" href="main.css">
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    <script src="image_gallery.js"></script>
</head>

<body>
<section>
    <h1>Image Gallery</h1>
    <ul id="image_list">
        <li><a href="images/casting1.jpg" title="Casting on the Upper Kings">Upper Kings</a></li>
        <li><a href="images/casting2.jpg" title="Casting on the Lower Kings">Lower Kings</a></li>
        <li><a href="images/catchrelease.jpg" title="Catch and Release on the Big Horn">Big Horn</a></li>
        <li><a href="images/fish.jpg" title="Catching on the South Fork">South Fork</a></li>
        <li><a href="images/lures.jpg" title="The Lures for Catching">Lures</a></li>
    </ul>
    <h2 id="caption">Casting on the Upper Kings</h2>
    <p id="gallery">
        <img src="images/casting1.jpg" alt="Image Gallery area" id="image">
    </p>
</section>
</body>
</html>

JS:

$(document).ready(function() {
    $("#image_list a").each(function() {
        // get the image URL and caption for each image
        var imageURL = $(this).attr("href");
        var caption = $(this).attr("title");

        // preload the image for each link      
        var galleryImage = new Image();
        galleryImage.src = imageURL;

        // set up the event handlers for each link
        $(this).click(function(evt) {
            $("#gallery img").slideUp(2000);
            $("#image").attr("src", imageURL);
            $("#caption").text(caption);

            // cancel the default action of each link
            evt.preventDefault();
        }); // end click 
    }); // end each
    // move the focus to the first link
    $("#image_list:first-child:first-child").focus();
}); // end ready

2 个答案:

答案 0 :(得分:1)

从每个循环中删除它:

[]

然后把它放在循环外面:

$(this).click(function(evt) {
    $("#gallery img").slideUp(2000);
    [....]
});

如果您想要幻灯片效果,请使用完整的事件:

$('#image_list a').on('click', function(){
    var imageURL = $(this).attr("href");
    var caption = $(this).attr("title");
    $("#image").attr("src", imageURL);
    $("#caption").text(caption);
    return false;
});

答案 1 :(得分:0)

我能够像这样解决它:

$(this).click(function(evt) {
    $("#gallery img").slideUp(2000, function(){
        $("#image").attr("src", imageURL);
        $("#caption").text(caption);
});

感谢您向我展示如何创建该功能,它解决了我的问题!