简体   繁体   English

悬停元素时更改背景

[英]Changing the background when hovering an element

I will preface this by saying that I am learning Javascript and Jquery.我会先说我正在学习 Javascript 和 Jquery。 I'm trying to make a minus that when the mouse hovers over an element, it changes the background image in the next column.我试图减去当鼠标悬停在元素上时,它会更改下一列中的背景图像。

For now I managed to compose the function and I wanted to insert also an animation when the image changes.现在我设法编写了 function 并且我还想在图像更改时插入 animation。

The problem I'm having is passing the mouse from one menu item to another.我遇到的问题是将鼠标从一个菜单项传递到另一个菜单项。 The transition does not apply.过渡不适用。 I've probably already identified the problem: the part of the function that restores the background when the mouse leaves the item.我可能已经发现了问题:当鼠标离开项目时,function 恢复背景的部分。

I can't find a solution, though.不过,我找不到解决方案。

Here is the code written so far:这是到目前为止编写的代码:

 $(function() { $('.item-1').hover(function() { $('.col-img').css('background-image', 'url(https://i1.wp.com/www.giacomocusano.com/wp-content/uploads/2016/07/coastal-wash-web.jpg?fit=1024%2C682&ssl=1)'); }); }); $(function() { $('.item-2').hover(function() { $('.col-img').css('background-image', 'url(https://www.chedonna.it/wp-content/uploads/2018/11/documentari.jpg'); }); }); $(function() { $('.item-3').hover(function() { $('.col-img').css('background-image', 'url(https://www.artemedialab.it/wp-content/uploads/2019/04/immagini-sfondo-1-700x400.jpg'); }); }); $(function() { $('.item-4').hover(function() { $('.col-img').css('background-image', 'url(https://www.radiomontecarlo.net/resizer/1200/720/true/cosmo-1592899369678.jpg--una_meravigliosa_farfalla_cosmica_si_aggira_nello_spazio__le_immagini_sono_mozzafiato.jpg?1592899369000'); }); });
 .dropdown-menu{ display:flex; align-items:center; height:100vh; }.col{ width:50%; padding: 20vw 10vw; }.col-img{ background-image: url(https://www.chimerarevo.com/wp-content/uploads/2020/04/immagini-gratis.jpg); height:100vh; transition: background 0.5s ease; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="dropdown-menu"> <div class="col col-img"> Col img </div> <div class="col col-menu"> <ul> <li><a href="#" class="item-1">Item 1</a></li> <li><a href="#" class="item-2">Item 2</a></li> <li><a href="#" class="item-3">Item 3</a></li> <li><a href="#" class="item-4">Item 4</a></li> </ul> </div> </div>

http://jsfiddle.net/ahL3kd7w/ http://jsfiddle.net/ahL3kd7w/

I think that your rather use data attribute to store the background url in your element.我认为您宁愿使用数据属性将背景 url 存储在您的元素中。 Like this, you can get it easily with jQuery later in your script.像这样,您可以稍后在脚本中使用 jQuery 轻松获得它。

https://api.jquery.com/data/ https://api.jquery.com/data/

You can use selector starting with specific name like $("[class^='item-']") .您可以使用以特定名称开头的选择器,例如$("[class^='item-']")

https://api.jquery.com/attribute-starts-with-selector/ https://api.jquery.com/attribute-starts-with-selector/

To make effect changing the background, you can use a call back to be sure to change the background url when animation is done.为了使更改背景生效,您可以使用回调确保在 animation 完成后更改背景 url。

https://api.jquery.com/fadeout/ https://api.jquery.com/fadeout/

 $(function() { $("[class^='item-']").hover(function() { let bgURL = $(this).data("imgurl"); // debug infos console.clear(); console.log(bgURL); $('.col-img').fadeOut("fast", function() { // Animation complete $('.col-img').css('background-image', 'url(' + bgURL + ')').fadeIn("fast"); }); }); });
 .dropdown-menu{ display:flex; align-items:center; height:100vh; }.col{ width:50%; padding: 20vw 10vw; }.col-img{ background-image: url(https://www.chimerarevo.com/wp-content/uploads/2020/04/immagini-gratis.jpg); height:100vh; transition: background 0.5s ease; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="dropdown-menu"> <div class="col col-img"> Col img </div> <div class="col col-menu"> <ul> <li><a href="#" class="item-1" data-imgurl="https://i1.wp.com/www.giacomocusano.com/wp-content/uploads/2016/07/coastal-wash-web.jpg?fit=1024%2C682&ssl=1">Item 1</a></li> <li><a href="#" class="item-2" data-imgurl="https://www.chedonna.it/wp-content/uploads/2018/11/documentari.jpg">Item 2</a></li> <li><a href="#" class="item-3" data-imgurl="https://www.artemedialab.it/wp-content/uploads/2019/04/immagini-sfondo-1-700x400.jpg">Item 3</a></li> <li><a href="#" class="item-4" data-imgurl="https://www.radiomontecarlo.net/resizer/1200/720/true/cosmo-1592899369678.jpg--una_meravigliosa_farfalla_cosmica_si_aggira_nello_spazio__le_immagini_sono_mozzafiato.jpg?1592899369000">Item 4</a></li> </ul> </div> </div>

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

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