jquery - JQuery .show() and .explode() Move the DIV Away
问题描述
I'm trying to use the show() and toggle() function on certain divs.
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<div style="width:100%; position:relative; top:10px; right:30px; color:white; font-size:40pt; line-height:1.5em;">
<div style="display: table; position:absolute; top:0px; width:100%;">
<div id="golden_particlesDiv" style="display: table-cell; text-align:center; display:none;">
<img src="https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__340.jpg" style="width:322px; height:322px;">
</div>
</div>
</div>
<script>
$(document).ready(function()
{
$("#golden_particlesDiv").show(2000).toggle( "explode", {pieces: 25}, 1000 );
});
</script>
</body>
</html>
JSFiddle: https://jsfiddle.net/un0g5xmz/
When I show a div using the .show function, it starts to fade in and move to the right till the center. And when I hide it using the toggle("explode"), it explodes and moves left! I want it to show up and explode without moving left or right. How can I do that?
解决方案
您必须针对爆炸动画img
而不是#golden_particlesDiv
针对爆炸动画,因为如果您使用相同#golden_particlesDiv
的爆炸动画,爆炸动画将从开始的位置#golden_particlesDiv
开始显示从身体左侧开始。
您可以通过在show
函数回调中切换动画来做到这一点,以便在show
动画完成时触发:
$("#golden_particlesDiv").show(2000, function() {
$(this).children('img').toggle( "explode", {pieces: 25}, 1000 );
});
下面的例子:
$(document).ready(function() {
$("#golden_particlesDiv").show(2000, function() {
$(this).children('img').toggle( "explode", {pieces: 25}, 1000 );
});
});
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<div style="width:calc(100%); position:relative; top:10px; right:30px; color:white; font-size:40pt; line-height:1.5em;">
<div style="display: table; position:absolute; top:0px; width:100%;">
<div id="golden_particlesDiv" style="display: table-cell; text-align:center; display:none;">
<img src="https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__340.jpg" style="width:322px; height:322px;">
</div>
</div>
</div>
</body>
</html>
推荐阅读
- graphics - OCaml 图形的窗口大小不正确
- python - 设置边框间距 matplotlib 图
- java - 如何在配置文件处于振动模式时使手机振动或在配置文件处于静音模式时从不响铃并在响铃模式下响铃
- python - 添加方法会产生“索引超出范围错误”?
- postgresql - PostgreSQL/pgAdmin4/dump 服务器版本不匹配使用 docker 镜像
- mysql - SequelizeJS Eager 加载两个相同的 N:M 模型一个返回数据另一个不
- python - 如何为 Python 编写 makefile(变成可执行文件)?
- javascript - 从 NPM 包中将 props 传递给函数
- c# - C#比较两个列表之间的特定项目属性
- html - 如何使用引导程序和 CSS 创建具有特定字体和背景颜色的图像?