如何让每个dom分别响应mouseenter()
How to have each dom respond to mouseenter() separately?
所以在看到所有组件都受到mouseenter()的影响后,我添加了。each(),但是因为这是一个图库…我只希望一个组件受到影响。
$(document).ready(function(){
$(".component").each(function(){
$(this).mouseenter(function(){
$(".primary").css({"margin-right":"0", "transition":"1s"});
$(".secondary").css({"margin-left":"0", "transition":"1s"});
});
$(this).mouseleave(function(){
$(".primary").css({"margin-right":"30px", "transition":"1s"});
$(".secondary").css({"margin-left":"30px", "transition":"1s"});
});
});
});
使用javascript中的事件传播机制
var theParent = document.querySelector("#idOfParent");
theParent.addEventListener("mouseenter", doYourStuffforMouseEnterEvent,false);
theParent.addEventListener("mouseleave", doYourStuffforMouseLeaveEvent,false);
function doYourStuffforMouseEnterEvent(e) {
if (e.target !== e.currentTarget) {
var Item = e.target.id;
alert("This item is the current hover item " + Item );
$(".primary").css({"margin-right":"0", "transition":"1s"});
$(".secondary").css({"margin-left":"0", "transition":"1s"});
});
}
e.stopPropagation();
}
function doYourStuffforMouseLeaveEvent(e) {
if (e.target !== e.currentTarget) {
var Item = e.target.id;
alert("This item is the current hover item " + Item );
$(".primary").css({"margin-right":"30px", "transition":"1s"});
$(".secondary").css({"margin-left":"30px", "transition":"1s"});
});
}
e.stopPropagation();
}
使用.children()
或.find()
:
$(document).ready(function() {
$(".component").each(function() {
$(this).mouseenter(function() {
$(this).children(".primary").css({
"margin-right": "0",
"transition": "1s"
});
$(this).children(".secondary").css({
"margin-left": "0",
"transition": "1s"
});
});
$(this).mouseleave(function() {
$(this).children(".primary").css({
"margin-right": "30px",
"transition": "1s"
});
$(this).children(".secondary").css({
"margin-left": "30px",
"transition": "1s"
});
});
});
});
* {
box-sizing: border-box;
}
.flex {
display: flex;
justify-content: center;
align-items: center;
}
.row {
flex-direction: row;
}
.column {
flex-direction: column;
}
.wrap {
flex-wrap: wrap;
}
body {
background: #ebebeb;
font-size: 1.25em;
font-family: 'Open Sans', sans-serif;
}
.container {
height: 100vh;
justify-content: space-around;
}
.component {} .primary {
background: red;
height: 150px;
width: 200px;
margin-right: 30px;
}
.secondary {
background: blue;
height: 100px;
width: 200px;
margin-left: 30px;
}
@media (max-width: 600px) {}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="container flex row wrap">
<div class="component flex column">
<div class="primary"></div>
<div class="secondary"></div>
</div>
<div class="component flex column">
<div class="primary"></div>
<div class="secondary"></div>
</div>
<div class="component flex column">
<div class="primary"></div>
<div class="secondary"></div>
</div>
<div class="component flex column">
<div class="primary"></div>
<div class="secondary"></div>
</div>
</div>
<!--container-->
相关文章:
- 如何将getJson的响应保存在全局变量中
- 如何使用Spring MVC将Facebook返回的响应数据保存在Java类中
- 使用angularjs向浏览器发送servlet响应(下载功能)
- MockJax没有在JavaScript应用程序中发送对我AJAX请求的响应
- 不再使用innerHTML进行AJAX响应.(代码未得到响应)
- 为什么可以't我使用了AJAX响应的一部分
- 响应动画手风琴不工作
- 如何从SeleniumWebdriver获取异步Javascript响应
- Ajax调用在Firefox中不会自动响应
- 可以在响应时隐藏iphone上的“播放”按钮以进行视频播放
- 由于响应中不存在“Access Control Allow Origin”标头,跨域请求停止工作
- 如何在 JavaScript 和 HTML 中更改对输入框的提示响应
- IIS动态HTTP响应标头
- 预期响应包含一个对象,但在angular js中得到一个数组错误
- Ajax响应转换
- 使Intro.js工具提示响应
- 存储$http如何从$http函数(Angularjs)外部获取可访问变量中的响应
- 将高图饼图中的文本居中显示为响应
- Css转换没有响应
- 如何让每个dom分别响应mouseenter()