3 回答
TA贡献1853条经验 获得超18个赞
请使用 css3 来实现您想要的结果,这是简单而有效的方法。
.myclass {
margin-top: 100px;
width: 100%;
}
.myclass:hover {
background: url("https://img.etimg.com/thumb/msid-68333505,width-643,imgsize-204154,resizemode-4/googlechrome.jpg");
}
<div class="myclass">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur
adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
TA贡献1798条经验 获得超3个赞
无需使用 jquery 或 javascript。您应该使用 CSS 完成此操作。
你需要做的是:
将您的更改img为div带有背景图像的
选择您#custom_image这是不 .active使用#custom_image:not(.active)
添加一个悬停其子#no_hover使用#no_hover:hover
这意味着以下 CSS 选择器:#custom_image:not(.active) #no_hover:hover.
要改变形象,如果#custom_image是.active,你必须选择孩子#no_hover的#custom_image这是.active。
您将使用 #custom_image.active #no_hover
// for example only:
document.getElementById('toggle-active').onclick = function() {
document.getElementById('custom_image').classList.toggle('active');
document.getElementById('active-info').innerHTML = document.getElementById('custom_image').classList.contains('active') ? 'active' : 'not active';
}
#no_hover {
width: 52px;
height: 58px;
padding-top: 12px;
background-image: url('https://via.placeholder.com/150/EEEEEE/EEEEEE');
background-size: cover;
}
#custom_image:not(.active) #no_hover:hover {
background-image: url('https://via.placeholder.com/150/0000FF/0000FF');
}
#custom_image.active #no_hover {
background-image: url('https://via.placeholder.com/150/0000FF/0000FF');
}
<!-- for example only -->
<button id="toggle-active">Toggle Active</button> #custom_image is <span id="active-info" style="font-weight: bold; color: red;">not active</span>
<hr />
<a href="#" class="pager-item" id="custom_image" data-slide-index="2">
<div class="single-how-app-work ">
<div class="icon-box">
<div class="inner" id="inner_id">
<!-- /.inner <i class="fas fa-dollar-sign"></i>-->
<!-- img src="https://via.placeholder.com/150/FFFFFF/000000" id="no_hover" style="width:52px;height:58px; padding-top:12px;" /-->
<div id="no_hover"></div>
</div>
<!-- /.inner -->
</div>
<!-- /.icon-box -->
<div class="text-box">
<h4>No Commission</h4>
<p>We use a customized application tobe <br /> specifically designed a testing gnose <br /> to keep away for people.</p>
</div>
<!-- /.text-box -->
</div>
</a>
以下代码段是一个使用字体真棒图标并使用类.pager-item
而不是 id的示例 #custom_image
。
// for example only:
document.getElementById('toggle-active').onclick = function() {
document.getElementById('custom_image').classList.toggle('active');
document.getElementById('active-info').innerHTML = document.getElementById('custom_image').classList.contains('active') ? 'active' : 'not active';
}
.pager-item .inner i {
font-size: 50px;
padding-top: 12px;
background-size: cover;
color: #0000FF;
}
.pager-item:not(.active) .inner i:hover {
color: #CCCCCC;
}
.pager-item.active .inner i {
color: #0000FF;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<!-- for example only -->
<button id="toggle-active">Toggle Active</button> .pager-item is <span id="active-info" style="font-weight: bold; color: red;">not active</span>
<hr />
<a href="#" class="pager-item" id="custom_image" data-slide-index="2">
<div class="single-how-app-work ">
<div class="icon-box">
<div class="inner" id="inner_id">
<i class="fa fa-stack-overflow"></i>
</div>
<!-- /.inner -->
</div>
<!-- /.icon-box -->
<div class="text-box">
<h4>No Commission</h4>
<p>We use a customized application tobe <br /> specifically designed a testing gnose <br /> to keep away for people.</p>
</div>
<!-- /.text-box -->
</div>
</a>
TA贡献1853条经验 获得超9个赞
你不需要使用任何 jQuery 来实现你想要的,这一切都可以通过 CSS 使用:hover伪类和类名来完成。
这是一个例子......正常情况下它会是粉红色的,将鼠标悬停在它上面是浅蓝色......然后如果你点击复选框,它会将activeClass类添加到<div>以便正常情况下它会是红色,带有悬停它是正常的蓝色。
jQuery 纯粹用于添加/删除类,但其他一切都只是 CSS ...
$(function(){
$("#changeClass").on("click", function() {
$("#myDiv").toggleClass("activeClass", $(this).is(":checked"));
});
});
#myDiv {
padding:50px;
/* Normal, pink */
background-color:pink;
}
#myDiv:hover {
/* Hover, lightblue */
background-color:lightblue;
}
#myDiv.activeClass {
/* Normal + active, red */
background-color:red;
}
#myDiv.activeClass:hover {
/* Hover + active, blue */
background-color:blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="changeClass" /><br/>
<div id="myDiv">
Hello World
</div>
添加回答
举报