【问题标题】:How do I change the mouseenter function to on click function for this image caption Jquery?如何将此图像标题 Jquery 的 mouseenter 功能更改为单击功能?
【发布时间】:2013-07-31 02:21:46
【问题描述】:

这是我以前用于显示图像的图像标题的代码。它适用于带有鼠标的设备,但我想稍微更新一下代码,以便它可以适应没有鼠标的用户。这是代码。

$(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 函数并将其与分配给页面链接的单击函数交换。我还希望此单击功能显示与图像关联的标题,并在第二次单击链接时隐藏标题。我尝试将它们换掉,但未能成功执行所需的结果。我对 JS 有点陌生,这是我唯一能想到的让这段代码符合我的意图的事情。有什么建议吗?

【问题讨论】:

  • 字幕代码在哪里?

标签: jquery function onclick click caption


【解决方案1】:

Working Demo

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

});

【讨论】:

  • 感谢您的回复。我尝试了您的代码,但由于某种原因它对我不起作用。这基本上是我在我的页面上所做的。 jsfiddle.net/hJMXa
  • 我不确定我是否正确地遵循了您的指示。
  • 嘿图沙尔。如果我有点不清楚,我很抱歉。我才意识到我的错误。标题是 img 标签中的替代文本。所以我想我不需要添加的标题切换? “点击我”链接应始终可见。想法?
猜你喜欢
  • 2019-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多