简体   繁体   English

将鼠标悬停在链接上并更改div的背景图片-JQuery

[英]Hover on a link and change the background-image of a div - JQuery

I'm having trouble making a div's background-image change when hovering over a link the code looks fine to me so I'm at a loss here is the code: 将鼠标悬停在链接上时,我无法更改div的背景图像,因此代码对我来说看起来不错,所以我很茫然,这里是代码:

Javascript: Javascript:

   $('#hover-01').on('mouseover', function(){
   $('#hover-change').css('background-image', 'url("images/1.jpg")');
});
$('#hover-01').on('mouseout', function(){, function(){
  $('#hover-change').css('background-image', 'url("images/5.jpg")');
});

HTML: HTML:

 <div class="open-project-link">
    <a id="hover-01" class="open-project"  
   href="project3.html">Bowman Clay</a>
    </div>


<div class="responsive-section-image" id="hover-change" 
 style="background-image: url(images/5.jpg);">
  <div class="overlay"></div>
                         </div>

jQuery version: v2.1.1 jQuery版本:v2.1.1

Any idea's or advice? 有什么想法或建议吗?

Edit: the code does work however it was a problem with a 3rd party plugin (I assume) so I fixed it with normal javascript and not jQuery 编辑:代码确实起作用,但是这是第三方插件(我认为)存在的问题,因此我使用普通的javascript而不是jQuery进行了修复

' mousein ' isn't an event handler that you can use. mousein ”不是您可以使用的事件处理程序。 You should use mouseover and mouseout , or mouseenter and mouseleave . 您应该使用mouseovermouseout ,或者mouseentermouseleave See jQuery mouse events here . 在此处查看jQuery鼠标事件。

You also need to give a width/height to your container that will hold the image, since it has no contents. 您还需要为容纳图像的容器指定宽度/高度,因为它没有内容。 Also, you have two function() declarations in your mouseout function, I fixed it in the following code sample: 另外,在mouseout函数中有两个function()声明,我在以下代码示例中对其进行了修复:

  $('#hover-01').on('mouseenter', function(){ $('#hover-change').css('background-image', 'url(http://www.w3schools.com/css/trolltunga.jpg)'); }); $('#hover-01').on('mouseleave', function(){ $('#hover-change').css('background-image', 'url(https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4)'); }); 
 #hover-change { width:1000px; height:300px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="open-project-link"> <a id="hover-01" class="open-project" href="project3.html">Bowman Clay</a> </div> <div class="responsive-section-image" id="hover-change"> <div class="overlay"></div> </div> 

Use this fiddle : 用这个小提琴

JS: JS:

$('#hover-01').on('mouseenter', function(){
   $('#hover-change').css('background-image', 'url("images/1.jpg")');
});
$('#hover-01').on('mouseout', function(){
  $('#hover-change').css('background-image', 'url("images/5.jpg")');
});

You can use jQuery's toggleClass() method to solve your problem like this: 您可以使用jQuery的toggleClass()方法来解决您的问题,如下所示:

 $(document).on('ready', function() { $('#link').on('mouseover', function() { //alert('sadasd'); $('body').toggleClass('colored'); }); $('#link').on('mouseleave', function() { //alert('sadasd'); $('body').toggleClass('colored'); }); }); 
 body.colored { background-color: #ccc; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <a id="link" href="#">This is a Link</a> </div> 

Hope this helps! 希望这可以帮助!

You can use the CSS :hover ~ to change the hover-change div when hover-01 is hovered over as follows: 当将hover-01时,可以使用CSS :hover ~来更改hover-change div,如下所示:

#divToHover:hover ~ #divToChange {
  /*your code here*/
}

 #change { height: 300px; width: 300px; background-image: url("//www.google.com/favicon.ico"); } #hover { height: 40px; width: 40px; background-color: green; } #hover:hover ~ #change { background-image: url(/favicon.ico); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> <div id="hover">hover</div> <div id="change"></div> 

Try the following code. 请尝试以下代码。 Make sure to set height and width for the div for which you want the background change. 确保为要更改背景的div设置高度和宽度。

Issue was with your event name used. 问题与您使用的事件名称有关。 Refer here for list of mouse events 请参阅此处以获取鼠标事件列表

 $('#hover-01').on('mouseover', function() { $('#hover-change').css('background-image', 'url("https://placehold.it/350x150/ffff00")'); }).on('mouseout', function() { $('#hover-change').css('background-image', 'url("https://placehold.it/350x150/ff0000")'); }); 
 .responsive-section-image { height: 150px; width: 350px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="open-project-link"> <a id="hover-01" class="open-project" href="project3.html">Bowman Clay</a> </div> <div class="responsive-section-image" id="hover-change" style="background-image: url('https://placehold.it/350x150/ff0000')"> <div class="overlay"></div> </div> 

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

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