[英]Change Background image of div on navbar hover
Please I am working on a project and I'm new to Javascript so I was wondering If there is a Jquery code or just a procedure on what to do to make the backgroung image to change on navigation menu hover. 请我正在一个项目上,我是Java的新手,所以我想知道是否有一个Jquery代码或只是一个如何使导航菜单悬停时更改背景图图像的过程。 eg hover on link one changes background image of div to image 1 hover on link two changes background image of div to image 2 Here is the HTML 例如,将鼠标悬停在链接1上将div的背景图像更改为图像1将鼠标悬停在链接2上将div的背景图像更改为图像2
<div class = "header-menu">
<div class ="pull-left">
<ul>
<li><a class="navigation" href="index.html">Home</a></li>
<li><a class="navigation" href="index.html">About</a></li>
<li><a class="navigation" href="index.html">Rules</a></li>
</ul>
</div>
<div class = "pull-right">
<ul>
<li> <a class="navigation" href="login.html">Log In </a></li>
<li><a class="navigation" href="signup.html">Sign Up</a></li>
</ul>
</div>
<div class = "logo-header">
<center><a href = "index.html"><img src = "images/logo.png" ></a></center>
</div>
</div>
<div class= "nav-tv">
</div>
and also the css 还有css
.header-menu{
height:95px;
width:100%;
color:black;
height:100px;
position:relative;
background-color:black;
overflow: hidden;
}
.header ul {
padding-top:35px;
}
.header-menu li {
display: inline;
margin :20px;
}
.pull-left{
padding-top:35px;
margin-top:0px;
float: left;
width = 33.3%;
}
.logo-header{
margin-top:15px;
position:absolute;
width:220px;
border: 1px solid black;
margin-left:570px;
margin-right:500px;
width = 33.3%;
}
.pull-right{
padding-top:35px;
width = 33.3%;
float :right;
}
.nav-tv{
width:100%;
height:350px;
border:1px solid black;
background-image: url('images/2.jpg');
background-repeat: no-repeat;
background-size: cover;
}
Something like this: 像这样:
$(document).ready(function(){
$("a.navigation#home").mouseover(function(){
$(.nav-tv).css("background-image", "url('images/your-new-bg.jpg')");
});
});
You need to set id's to your navigation: 您需要为导航设置ID:
<a class="navigation" id="home" href="index.html">Home</a>
This can be done using pure CSS. 这可以使用纯CSS完成。 Using the :hover
pseudo-class, you can change pretty much anything you want. 使用:hover
伪类,您几乎可以更改任何所需的内容。
.header-menu li:hover {
background-image: url('');
}
The code would be: 该代码将是:
$(function(){
$('#id1').hover(function() {
$('css selector of div').css('background-image', imageUrl);
});
$('#id2').hover(function() {
$('css selector of div').css('background-image', imageUrl);
});
$('#id3').hover(function() {
$('css selector of div').css('background-image', imageUrl);
});
});
Or if you have an incremental number you can just do this: 或者,如果您有递增数量,则可以执行以下操作:
$(function(){
for(i=1; i<=5; i++) {
$('#id' + i).hover(function() {
$('css selector of div').css('background-image', imageUrl);
});
}
});
If you don't want to use jQuery, you can do it adding for example data-image to .navigation elements, and set onmouseover event, to catch element which was hovered, get it's data-image attribute and set its value to nav-tv's background image 如果您不想使用jQuery,可以这样做,例如将data-image添加到.navigation元素,并设置onmouseover事件,以捕获被悬停的元素,获取它的data-image属性并将其值设置为nav-电视的背景图片
html: 的HTML:
<a data-image='images/1.jpg' class="navigation" href="index.html">Home</a>
Javascript: Javascript:
(function() {
var navTv = document.getElementById('nav-tv');
var arr = document.getElementsByClassName('navigation');
for(var i=0; i<arr.length; i++) {
arr[i].onmouseover = function(e) {
var a = e.target;
var imgSrc = a.getAttribute('data-image');
var style = ['background-image: url(', imgSrc, ');'].join('');
navTv.setAttribute('background-image', style);
}
}
})();
Working example: https://jsfiddle.net/2npp6t6q/1/ 工作示例: https : //jsfiddle.net/2npp6t6q/1/
Or this: https://jsfiddle.net/1e2v71pr/ 或这样: https : //jsfiddle.net/1e2v71pr/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.