[英]How do I change the mouseenter function to on click function for this image caption Jquery?
这是我之前用来显示图像标题的代码。 它非常适合带有鼠标的设备,但我想稍微更新一下代码,使其适合没有鼠标的用户。 这是代码。
$(function() {
$(".thumb").mouseenter(function() {
var $t = $(this);
var $d = $("<div>");
$d.addClass("desc").text($t.attr("alt")).css({
width: $t.width(),
height: $t.height() - 20,
top: $t.position().top
});
$t.after($d).fadeTo("fast", 0.3);
$d.mouseleave(function() {
$(this).fadeOut("fast", 0, function() {
$(this).remove();
}).siblings("img.thumb").fadeTo("fast", 1.0);
});
});
});
基本上,我想更改mouseenter / mouseleave函数,并将其与分配给页面上链接的click函数交换。 我还希望该单击功能显示与图像相关的标题,并在第二次单击链接时隐藏该标题。 我尝试过仅将它们换出,但无法成功执行所需的结果。 我对JS有点陌生,这是我能想到的唯一使这部分代码起作用的事情。 有什么建议么?
$(function () {
$(".thumb").click(function () { //replaced to click
var $t = $(this);
$d = $("<div>");
$d.addClass("desc").text($t.attr("alt")).css({
width: $t.width(),
height: $t.height() - 20,
top: $t.position().top
});
//added caption toggle
$t.after($d).fadeTo("fast", 0.3);
$d.click(function () { //replaced to click
$(this).fadeOut("fast", 0, function () {
$(this).remove();
}).siblings("img.thumb").fadeTo("fast", 1.0);
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.