【发布时间】:2015-11-16 18:52:27
【问题描述】:
我正在尝试创建一个电子商务项目库。我希望它能够显示选择出现在灯箱中的图像。一切正常,除了当用户选择第二个缩略图时,它不会显示在灯箱中。灯箱仅显示第一个图像 href。在您选择另一个缩略图后,似乎“href”没有正确交换。通过“alt”属性传递的标题也会发生这种情况。
这是我的代码。
$(function() {
$(".image").click(function() {
var image = $(this).attr("rel");
$('#image img').fadeOut("slow", function(){
$('#image a').html('<img src="' + image + '"/>');
$('#image a').fadeIn('slow');
});
return false;
});
});
var $overlay = $('<div id="overlay"></div>'); //Stores Jquery Handler within a Variable.
var $image = $("<img>");
var $caption = $("<p></p>");
// Add Image to overlay
$overlay.append($image);
// Add Caption
$overlay.append($caption);
// Add overlay
$("body").append($overlay);
// Capture the click event on a link to an image.
$("#image a").click(function(event){
event.preventDefault(); //Prevents the browser from loading the link in a new page!
var imageLocation = $(this).attr("href");
// Update overlay with the image linked in the link.
$image.attr("src", imageLocation);
// Show the overlay.
//console.log(href);
$overlay.show();
// Get Childs Alt attribute and set caption. (Childs Image Alt Attribute)
var captionText = $(this).children("img").attr("alt");
$caption.text(captionText);
});
//3. When overlay is clicked
$overlay.click(function(){
//3.1 Hide the overlay
$overlay.hide();
});
这是Demo
【问题讨论】:
标签: javascript jquery html css image