简体   繁体   English

悬停链接时更改其他 div 背景

[英]When hover a link change other div background

I have two div s, one with a list of links (projects) and another empty.我有两个div ,一个带有链接(项目)列表,另一个是空的。 My goal is to whenever I hover a link on the first div , the background of the second div changes to the project corresponding image.我的目标是每当我将链接悬停在第一个div ,第二个div的背景就会更改为项目对应的图像。 Here is an example of what I'm trying to accomplish https://www.humbertpoyet.com/about/press and here is my code.这是我正在尝试完成的示例https://www.humbertpoyet.com/about/press ,这是我的代码。 Thank you谢谢

HTML HTML

<div class="wrapper">
    <div class="grid-container">
        <div class="item6">
            <ul>
                <li><h3>Case Studys</h3></li>
                <li><a href="#">Project 1</a></li>
                <li><a href="#">Project 2</a></li>
                <li><a href="#">Project 3</a></li>
                <li><a href="#">Project 4</a></li>
            </ul>
        </div>
        <div class="item6 thumb">
    </div>
</div>

CSS CSS

.item6 {grid-column: span 6;}
.thumb {background-size: cover; background-position: center; background-repeat: no-repeat;}

You can use the following jQuery function.您可以使用以下 jQuery 函数。 Note that you have to set a height for .thumb , otherwise it will be invisble due to no content, which results in height 0.请注意,您必须为.thumb设置一个height ,否则由于没有内容,它将不可见,从而导致高度为 0。

 $('.item6>ul>li:nth-child(2)').hover(function() { $('.item6.thumb').css('background-image', 'url(https://placehold.it/600x400/fa0)'); }); $('.item6>ul>li:nth-child(3)').hover(function() { $('.item6.thumb').css('background-image', 'url(https://placehold.it/800x300/09b)'); }); $('.item6>ul>li:nth-child(4)').hover(function() { $('.item6.thumb').css('background-image', 'url(https://placehold.it/300x500/d2c)'); }); $('.item6>ul>li:nth-child(5)').hover(function() { $('.item6.thumb').css('background-image', 'url(https://placehold.it/100x300/aa4)'); });
 .item6 { grid-column: span 6; } .thumb { height: 300px; background-size: cover; background-position: center; background-repeat: no-repeat; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="wrapper"> <div class="grid-container"> <div class="item6"> <ul> <li> <h3>Case Studys</h3> </li> <li><a href="#">Project 1</a></li> <li><a href="#">Project 2</a></li> <li><a href="#">Project 3</a></li> <li><a href="#">Project 4</a></li> </ul> </div> <div class="item6 thumb"> </div> </div>

If you add data-url to your list items, you can use JQuery to capture when the user hovers above the list item, take the desired image and replace the background of the second div.如果将 data-url 添加到列表项中,则可以使用 JQuery 捕获用户悬停在列表项上方的时间,获取所需图像并替换第二个 div 的背景。

<li data-url='url("paper.gif")'><h3>Case Studys</h3></li>


$(".item6").on("mouseenter", "li", function(){
    let imageUrl = $(this).attr("data-url");
    $(".item6.tumb").css("background-image", imageUrl);
});

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

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