鼠标悬停时显示图像(在带有 jsfiddle 的工作示例中)

Display Image on Mouseover (inside working example with jsfiddle)

本文关键字:工作 jsfiddle 显示 悬停 显示图 图像 鼠标      更新时间:2023-09-26

我发现了这个小提琴 http://jsfiddle.net/22sxotje/如果我将鼠标悬停在带有类 lochtonialcaress 的元素上,则会出现图像lochtonialcaress.png

我如何更改此脚本以显示任何带有类名的元素的图像(类名.png),但没有图像classname.png则不显示任何

内容

如果我复制它并仅更改类名,这是有效的,但是如何将它们连接在一起:

$(document).ready(function () {
    var yOff = 15;
    var xOff = -20;
    var pathToImage = "http://x.jupe.pl/lochtonialcaress.png";
    $(".lochtonialcaress").hover(function (e) {
        $("body").append("<p id='image-when-hovering-text'><img src='" + pathToImage + "'/></p>");
        $("#image-when-hovering-text")
            .css("position", "absolute")
            .css("top", (e.pageY - yOff) + "px")
            .css("left", (e.pageX + xOff) + "px")
            .fadeIn("fast");
    },
    function () {
        $("#image-when-hovering-text").remove();
    });
    $(".lochtonialcaress").mousemove(function (e) {
        $("#image-when-hovering-text")
            .css("top", (e.pageY - yOff) + "px")
            .css("left", (e.pageX + xOff) + "px");
    });
});
$(document).ready(function () {
    var yOff = 15;
    var xOff = -20;
    var pathToImage = "http://x.jupe.pl/vollsprotector.png";
    $(".vollsprotector").hover(function (e) {
        $("body").append("<p id='image-when-hovering-text'><img src='" + pathToImage + "'/></p>");
        $("#image-when-hovering-text")
            .css("position", "absolute")
            .css("top", (e.pageY - yOff) + "px")
            .css("left", (e.pageX + xOff) + "px")
            .fadeIn("fast");
    },
    function () {
        $("#image-when-hovering-text").remove();
    });
    $(".vollsprotector").mousemove(function (e) {
        $("#image-when-hovering-text")
            .css("top", (e.pageY - yOff) + "px")
            .css("left", (e.pageX + xOff) + "px");
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>This is my paragraph with a <a class="lochtonialcaress" href="#">very special hover</a> effect on the link.</p>
<p><span class="vollsprotector">Lorem</span> ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirm od tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
<p>The hovering works with every element that has <span class="no-image"> a class of <em>"text-hover-image"</em></span>

您将在此小提琴中找到一个工作示例。您只需要添加一个if语句即可执行此操作。

$(".lochtonialcaress").hover(function (e) {
    if (pathToImage) {
    $("body").append("<p id='image-when-hovering-text'><img src='" + pathToImage + "'/></p>");
    $("#image-when-hovering-text")
        .css("position", "absolute")
        .css("top", (e.pageY - yOff) + "px")
        .css("left", (e.pageX + xOff) + "px")
        .fadeIn("fast");
    }
},

而不是:

$(".lochtonialcaress").hover(function (e) {
    $("body").append("<p id='image-when-hovering-text'><img src='" + pathToImage + "'/></p>");
    $("#image-when-hovering-text")
        .css("position", "absolute")
        .css("top", (e.pageY - yOff) + "px")
        .css("left", (e.pageX + xOff) + "px")
        .fadeIn("fast");
},