单击jQuery标志动画
我想制作一个节目,在那里旗杆旁边有一面旗帜。它从杆的底部开始,当用户单击它时,它会移动到杆的顶部。当它位于顶部时,如果用户再次单击它,它将移动到底部。所以我试图使国旗的效果。但现在我有了第一部分的工作,它向上移动。但向下移动的部分并非如此。有人能帮忙吗?谢谢单击jQuery标志动画,jquery,position,Jquery,Position,我想制作一个节目,在那里旗杆旁边有一面旗帜。它从杆的底部开始,当用户单击它时,它会移动到杆的顶部。当它位于顶部时,如果用户再次单击它,它将移动到底部。所以我试图使国旗的效果。但现在我有了第一部分的工作,它向上移动。但向下移动的部分并非如此。有人能帮忙吗?谢谢 <!DOCTYPE html> <html> <head> <script src = "https://ajax.googleapis.com/ajax/libs
<!DOCTYPE html>
<html>
<head>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">.
</script>
</head>
<body>
<img src = "https://upload.wikimedia.org/wikipedia/en/thumb/6/6c/Us_flag_large_38_stars.png/1200px-Us_flag_large_38_stars.png" id = "flag" width = "200px" height = "140px">
<div id = "pole"></div>
<script>
//booleans
var reachedTop = false;
var reachedBottom = true;
//initial positioning
$("#pole").width(10);
$("#pole").height($(document).height());
$("#pole").css("background-color", "gray");
$("#pole").css({left: $(document).width() * 5/12});
$("#pole").css("position", "absolute");
$("#flag").css("position", "absolute");
$("#flag").css({left: $("#pole").position().left, top: $(document).height()/3});
//animations on clicks
if (reachedBottom && reachedTop == false){
$("#flag").click(function(){
$("#flag").animate({top: $("#pole").position().top});
if ($("#flag").position().top == $("#pole").position().top){
reachedTop = true;
reachedBottom = false;
}
});
} else if (reachedTop && reachedBottom == false){
$("#flag").click(function(){
$("#flag").animate({top: $(document).height()/3});
if ($("#flag").position().top == $(document).height()/3){
reachedTop = false;
reachedBottom = true;
}
});
}
</script>
</body>
.
//布尔人
var-reachedTop=false;
var reachedBottom=true;
//初始定位
$(“#杆”)。宽度(10);
$(“#杆”).height($(文档).height());
$(“#极”).css(“背景色”、“灰色”);
$(“#pole”).css({左:$(document.width()*5/12});
$(“极点”).css(“位置”、“绝对”);
$(“#标志”).css(“位置”、“绝对”);
$(“#flag”).css({left:$(“#pole”).position().left,top:$(document.height()/3});
//单击时的动画
if(reachedbooth&&reachedTop==false){
$(“#标志”)。单击(函数(){
$(“#flag”).animate({top:$(“#pole”).position().top});
if($(“#flag”).position().top==$(“#pole”).position().top){
reachedTop=true;
到达底部=错误;
}
});
}else if(reachedTop&&reachedtown==false){
$(“#标志”)。单击(函数(){
$(“#flag”).animate({top:$(document.height()/3});
if($(“#标志”).position().top==$(文档).height()/3){
reachedTop=false;
到达底部=真;
}
});
}
我实际上会使用CSS和JavaScript,只是为了更新类,让CSS控制动画
$(函数(){
$(“.pole”)。单击(函数(){
$(this.toggleClass(“top”);
});
});代码>
.pole{
高度:500px;
宽度:5px;
背景色:#000;
位置:相对位置;
}
.pole img{
位置:绝对位置;
宽度:150px;
左:5px;
顶部:100px;
过渡:顶部0.25s线性;
}
.杆顶img{
排名:0;
}