简体   繁体   English

如何使用js在链接悬停时显示背景图像

[英]How to display background image on link hover using js

I am attempting to create image display behind text on link hover.我试图在链接悬停时在文本后面创建图像显示。 The problems I am having is the positioning of the image - I want them to appear at the top of the browser and the second issue is the other text elements with h1 tag move when the image is being displayed我遇到的问题是图像的定位 - 我希望它们出现在浏览器的顶部,第二个问题是其他带有 h1 标签的文本元素在显示图像时移动

here is what I have so far https://jsfiddle.net/6sy1drLw/3/ Any suggestions would be much appreciated这是我到目前为止所拥有的https://jsfiddle.net/6sy1drLw/3/任何建议将不胜感激

JS JS

$('.onhover-toggle-child-class').on(
'mouseenter mouseleave',
function() {
var element = $(this);
var selector = element.data('target');
var child = element.find(selector);
var classes = element.data('toggle');


child.toggleClass(classes);
}
);

Looks like the HTML you are defining in your JSFiddle example is not well constructed.看起来您在 JSFiddle 示例中定义的 HTML 没有很好地构建。

The h1 tag and in general heading elements should wrap only text, but you are putting anchor elements inside it. h1标签和一般的标题元素应该只包装文本,但您将锚元素放在其中。 That is the reason why it moves when the image appears, because it's height is being altered by your classes swapping.这就是当图像出现时它移动的原因,因为它的高度正在被你的类交换改变。 Have a look at Semantics .看看语义

In regards to your issue with the background image positioning, it is not being displayed at the top of the browser because the main parent container is position: relative , so the image is absolute in regards to the parent position and not the window.关于您的背景图像定位问题,它没有显示在浏览器的顶部,因为主父容器是 position: relative ,因此图像对于父位置而不是窗口是绝对的。

Consider refactor your code taking into account the points above and share it so we can figure out this better.考虑重构你的代码,考虑到上述几点并分享它,以便我们可以更好地解决这个问题。

--- ANSWER UPDATE --- ---答案更新---

Hey @Sasha.Burger,嘿@Sasha.Burger,

Take a look at this example, I hope it can help:看看这个例子,我希望它可以帮助:

 $('.hoverable__text').on('mouseenter mouseleave', function(event) { var $hoverableContainer = $('#hoverable-background'); var toggleClass = event.target.getAttribute('toggle'); $hoverableContainer.toggleClass(toggleClass); });
 * { box-sizing: border-box; margin: 0; } body { font-family: arial; } .hoverable { height: 100vh; padding: 20px; width: 100vw; } .hoverable-option-1 { background: url("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR24qtJ7yiZutbUs9MtLQUmWN9jF6j2fLQTov8_qRCgRMWRPRjm"); background-repeat: no-repeat; } .hoverable-option-2 { background: url("https://f4.bcbits.com/img/0013854064_10.jpg"); background-repeat: no-repeat; } .hoverable-option-3 { background: url("https://f4.bcbits.com/img/0013854064_10.jpg"); background-repeat: no-repeat; } .hoverable__container { display: block; margin: 0 auto; width: 80%; } .hoverable__text { cursor: pointer; font-weight: bold; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="hoverable" id="hoverable-background"> <div class="hoverable__container"> <p> Lorem ipsum dolor spansit <span toggle="hoverable-option-1" class="hoverable__text">SHOW IMAGE</span> amet, consectetur adipisicing elit. Quasi saepe voluptatem quam est <span toggle="hoverable-option-2" class="hoverable__text">SHOW IMAGE</span> dolores quis quaerat ex, aspernatur aperiam voluptas voluptatum eos corrupti deleniti ad obcaecati? Doloribus <span toggle="hoverable-option-3" class="hoverable__text">SHOW IMAGE</span> cupiditate velit ratione. </p> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM