Jquery 将 x 移动到 y 并消失
Jquery move x to y and disappear
>我得到了这 3 个带有按钮的元素,我需要元素移动到目标 (X) 并随着点击消失。第二个提示,我如何才能只移动按下的div?现在所有的div都采用动画。
嗨,写了这个代码笔,我的元素只是采用不透明度的属性,但我无法移动它。
重要提示:所有元素都应达到目标 X,甚至是第二个和第三个。
代码笔示例
$(document).ready(function(){
$("button").click(function(){
$("div").animate({
left: '250px',
opacity: '0.5'
});
});
});
.try {
background: red;
margin-bottom: 10px;
height: 100px;
width: 100px;
}
.target {
font-size: 50px;
float: right;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<div class="target"> X</div>
<div class="try"><button id="play">Start Animation</button></div>
<div class="try"><button id="play">Start Animation</button></div>
<div class="try"><button id="play">Start Animation</button></div>
更新的代码笔
这可以通过在div 中添加position:absolute
来完成:
$(document).ready(function(){
$("button").click(function(){
var current_div = $(this).closest(".try");
current_div.animate({
left: '85%',
opacity: '0.5',
top: '0px',
margin: '0px'
}, 2000, function(){
current_div.hide();
});
});
});
.try {
background: red;
margin-bottom: 10px;
height: 100px;
width: 100px;
position: absolute;
}
.target {
font-size: 50px;
float: right;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="target"> X</div>
<div class="try"><button id="play">Start Animation</button></div>
<div class="try" style="margin-top:105px"><button id="play">Start Animation</button></div>
<div class="try" style="margin-top:210px"><button id="play">Start Animation</button></div>
您可以通过
获取X
元素的位置,将要移动到absolute
位置的元素的css更改为定位(.try
),然后在jQuery的animate的回调中设置不透明度来实现您正在寻找的功能:
$('.play').on('click', function() {
var clickedButton = $(this),
parentDiv = $(this).parent('.try'),
targetPosition = $('.target').position(),
targetLeft = targetPosition.left,
targetTop = targetPosition.top;
$(parentDiv).css({
"position": "absolute"
})
.animate({
left: targetLeft,
top: targetTop
}, 1000, "linear", function() {
$(this).css({
"opacity": "0.5"
});
});
});
.try {
background: red;
margin-bottom: 10px;
height: 100px;
width: 100px;
}
.target {
font-size: 50px;
float: right;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="target">X</div>
<div class="try">
<button class="play">Start Animation</button>
</div>
<div class="try">
<button class="play">Start Animation</button>
</div>
<div class="try">
<button class="play">Start Animation</button>
</div>
最后,您不想使用同一id
的多个实例,因此只需将#play
更改为.play
:
<div class="target"> X</div>
<div class="try"><button class="play">Start Animation</button></div>
<div class="try"><button class="play">Start Animation</button></div>
<div class="try"><button class="play">Start Animation</button></div>
相关文章:
- 当移动地址栏出现/消失时,防止更改窗口高度
- 如何确保 php 变量在移动到 java 页面时不会消失
- 当鼠标在图像上突然移动时,如何阻止图像消失
- CSS js 在鼠标移动重绘时不会消失
- Jquery 将 x 移动到 y 并消失
- Jquery移动页面在使用css显示图像后消失
- 移动页脚,当您到达终点时消失
- 当在移动设备/平板电脑上单击某个项目时,使菜单消失
- 当添加ASP代码时,导航在移动中消失,其他脚本在页面上冻结
- 下拉列表在用户可以将鼠标指针移动到它之前消失
- 菜单文本在从移动设备调整到桌面后消失
- 为什么每次精灵移动时我的地图都会消失
- 其中'azure移动服务中的CORS设置已经消失了
- html5画布背景图像在鼠标移动时消失
- 是否有一个Bootstrap类的东西只在移动设备上消失
- JQuery移动列表视图元素消失
- 移动菜单在滚动时消失
- 元标签视窗使整个页面在移动设备上消失
- CSS在移动设备上消失
- 移动导航在滚动时消失