简体   繁体   English

悬停链接时添加背景的背景图像

[英]Add background-image of background when hovering a link

I've the following code: 我有以下代码:

<div class="container">
   // some other code
   <ul>
      <li><a href="#about">about</a></li>
      <li><a href="#service">service</a></li>
      <li><a href="#contact">contact</a></li>
   </ul>
</div>

The <a> links are styled as buttons. <a>链接的样式为按钮。
Now I want to achieve, when I hovering the <a> link, I will add a background image to the container-div. 现在我想实现,当我将<a>悬停在<a>链接上时,我将在容器-div中添加一个背景图像。 But each of the <li> 's have their own image. 但是每个<li>都有自己的图像。 So when I hover the "about" link, the container div get's a "about"-image. 因此,当我将“ about”链接悬停时,容器div会得到一个“ about”图像。 When I hover the "service" link, the container div get's a "service"-image. 当我将鼠标悬停在“服务”链接上时,容器div会得到一个“服务”图像。

How can I achieve that? 我该如何实现?

What I already tried: 我已经尝试过的
.container ul li a:hover .container{ background-image: url('path/to/image) }
.container ul li a:hover > .container{ background-image: url('path/to/image) }
.container ul li a:hover ~ .container{ background-image: url('path/to/image) }
.container ul li a:hover + .container{ background-image: url('path/to/image) }

WHEN IT IS NOT POSSIBLE WITH ONLY CSS, Is there a way with JavaScript / jQuery? 如果仅CSS不可行,JavaScript / jQuery是否有办法?

I don't think it is possible using css alone 我认为单独使用CSS是不可能的

<div class="container">// some other code
    <ul>
        <li>
            <a href="#about" data-type="about">about</a>
        </li>
        <li>
            <a href="#service" data-type="service">service</a>
        </li>
        <li>
            <a href="#contact" data-type="contact">contact</a>
        </li>
    </ul>
</div>

then 然后

.container.about{
    background-image: url(...)
}

and

jQuery(function ($) {
    $('.container li a').hover(function () {
        $('.container').addClass($(this).data('type'))
    }, function () {
        $('.container').removeClass($(this).data('type'))
    })
})

Demo: Fiddle 演示: 小提琴

jQuery: jQuery的:

$('.container > ul > li > a').each(function() {
    $(this).hover(function() {
        $('.container').toggleClass(this.hash.replace('#', ''));
    });
});

CSS: CSS:

.about { background-image: url('path/to/image1') }
.service { background-image: url('path/to/image2') }
.contact { background-image: url('path/to/image3') }
<div class="container">
   // some other code
   <ul>
      <li><a href="#about" id="1">about</a></li>
      <li><a href="#service" id="2">service</a></li>
      <li><a href="#contact" id="3">contact</a></li>
   </ul>
</div>




$(document).ready(function() {

    $('#1').hover(function(){
    $('.container').css("background","url(images/pic1.jpg)");  
}, function () {
     $('.container').css("background","'none'");  
});
    $('#2').hover(function(){
    $('.container').css("background","url(images/pic2.jpg)");  
}, function () {
     $('.container').css("background","'none'");  
});
    $('#3').hover(function(){
    $('.container').css("background","url(images/pic3.jpg)");  
}, function () {
     $('.container').css("background","'none'");  
});

 });

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

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