javascript - 使用 jquery mouseover 事件在单个元素中添加 2 个 css
问题描述
顶部大小将是动态的,所以我应该在 jquery 上初始化它,而不是在默认的 css 样式上。有谁知道该怎么做?
`
$('.box').hover(function(){
/*I need to inialize the $top size here before proceeding to the below css*/
$('.hover',this).css({'top':'0'});
});
.box {
width: 200px;
height: 200px;
border: 2px solid red;
margin: 10% auto;
position: relative;
overflow: hidden;
}
.hover {
position: absolute;
width: 100%;
height: 100%;
opacity: 0.5;
background: blue;
transition: all 300ms ease 0s;
}
img {
width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="box">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ_zAA8810ONGbOaB_F5hZ1prrTnC749ko6otAfyltJBSCKPgIm" alt="img">
<div class="hover"></div>
</div>
</div>
`
解决方案
你不需要Javascript。
.box {
width: 300px;
height: 300px;
border: 2px solid red;
margin: 10% auto;
position: relative;
overflow: hidden;
}
.content {
display: none;
position: absolute;
background: green;
width: 100%;
height: 100%;
transition: all 300ms ease 0s;
display:block;
top:0%;
}
.box:hover .content {
top:-100%;
}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Enter Leave Over Out</title>
</head>
<body>
<div class="container">
<div class="box">
<div class="content"></div>
</div>
</div>
</body>
</html>
推荐阅读
- typescript - 如何收听来自套接字的消息并将它们打印到 TypeScript 中的控制台?
- c# - ASP.Net Core Web App - cshtml 文件无法调用控制器方法
- visual-studio-2019 - 有没有办法在 launchSettings.json 中使用构建变量?
- aws-lambda - Alexa.getRequestType 不是 AWS lambda Alexa 技能中的函数
- visual-studio-code - 如何在 VS Code 中添加文本限制行?
- c++ - C++ 排序的对象集
- java - Spring Boot 应用在部署到 Google App Engine 时无法连接到 Google Cloud SQL (PostgreSQL)
- visual-studio - EF Core 是否对 SSMS 视图进行反向工程,而不仅仅是表?
- layout - tikz:裁剪范围的相对定位
- python - 对列表列表进行排序以查找共享变体