滑动后无法将 div 移回原始位置
Unable to move div back to original position after slide
我的滑动开关有问题。单击后的黑框将第二个 div 滑回其中很好,但它本身不会滑回其原始位置。我怎样才能解决这个问题?
我目前正在使用 Jquery UI。
代码如下:
#recommended-product-block{
position: absolute;
top:225px;
right:0;
z-index: 999999;
width:100px;
}
#weather{
width:150px;
position:absolute;
right: 0;
}
<div id="recommended-product-block">
<h1>Div box to initiate slide toggle</h1>
</div>
<div id="weather">
<h1>Box that slides in and out</h1>
</div>
$(document).ready(function() {
$('#weather').hide();
$(function () {
$("#recommended-product-block").click(function () {
$(this).animate({ right: '+150' }, 400 );
$( "#weather" ).toggle( "slide" );
});
});
});
好吧,如果您要在两个 <div>
标签之间切换,那非常简单。首先 div 将被隐藏。单击第二个 div,第一个 div 将滑动,第二个将消失。单击第一个 div 第二个 div 将滑动,第一个将消失。
$('#weather').hide();
$(function () {
$("#recommended-product-block").click(function () {
toggle();
});
$("#weather").click(function () {
toggle();
});
});
function toggle(){
$( "#weather" ).toggle('slide');
$( "#recommended-product-block" ).toggle('slide');
}
(为清楚起见添加了边框)
编辑:
要控制速度,请使用此切换功能
function toggle(){
$('#weather').toggle('slide', {direction:'left'}, 300);
$( "#recommended-product-block" ).toggle('slide', {direction:'left'}, 300);
}
编辑(根据评论)
$('#weather').hide();
$(function () {
var notClicked = true;
$("#recommended-product-block").click(function () {
$( "#weather" ).toggle('slide', 400);
if(notClicked) {
$(this).animate({'right':150}, 400);
}
else
{
$(this).animate({'right':0},400);
}
notClicked = !notClicked;
});
});
我的滑动开关有问题。单击后的黑框将第二个 div 滑回其中很好,但它本身不会滑回其原始位置。我怎样才能解决这个问题?
我目前正在使用 Jquery UI。
代码如下:
#recommended-product-block{
position: absolute;
top:225px;
right:0;
z-index: 999999;
width:100px;
}
#weather{
width:150px;
position:absolute;
right: 0;
}
<div id="recommended-product-block">
<h1>Div box to initiate slide toggle</h1>
</div>
<div id="weather">
<h1>Box that slides in and out</h1>
</div>
$(document).ready(function() {
$('#weather').hide();
$(function () {
$("#recommended-product-block").click(function () {
$(this).animate({ right: '+150' }, 400 );
$( "#weather" ).toggle( "slide" );
});
});
});
好吧,如果您要在两个 <div>
标签之间切换,那非常简单。首先 div 将被隐藏。单击第二个 div,第一个 div 将滑动,第二个将消失。单击第一个 div 第二个 div 将滑动,第一个将消失。
$('#weather').hide();
$(function () {
$("#recommended-product-block").click(function () {
toggle();
});
$("#weather").click(function () {
toggle();
});
});
function toggle(){
$( "#weather" ).toggle('slide');
$( "#recommended-product-block" ).toggle('slide');
}
(为清楚起见添加了边框)
编辑:
要控制速度,请使用此切换功能
function toggle(){
$('#weather').toggle('slide', {direction:'left'}, 300);
$( "#recommended-product-block" ).toggle('slide', {direction:'left'}, 300);
}
编辑(根据评论)
$('#weather').hide();
$(function () {
var notClicked = true;
$("#recommended-product-block").click(function () {
$( "#weather" ).toggle('slide', 400);
if(notClicked) {
$(this).animate({'right':150}, 400);
}
else
{
$(this).animate({'right':0},400);
}
notClicked = !notClicked;
});
});