【发布时间】:2021-06-05 11:42:21
【问题描述】:
我正在尝试制作一个简单的游戏,其中当单击GO 按钮时,一艘船从一个海岸驶向另一个海岸。我尝试使用 jQuery animate,但没有按预期工作。
哈巴狗
body
button.go GO
.main
.left
.seaShore
.travel
.boat
.river
.right
.seaShore
萨斯
*
margin: 0
padding: 0
box-sizing: border-box
@mixin flex($justify,$align)
display: flex
justify-content: $justify
align-items: $align
.go
padding: 5px 10px
position: fixed
top: 20px
right: 50%
transform: translateX(50%)
.main
height: 100vh
@include flex(center,flex-end)
.seaShore
background-color: green
width: 25vw
height: 300px
.travel
position: relative
.boat
position: absolute
top: -15px
right: 0
width: 200px
height: 40px
border-radius: 0 0 50% 50%
background-color: brown
.river
width: 50vw
height: 290px
background-color: #00bfff
jQuery
$(document).ready(function () {
let right = false;
let offset = $('.boat').offset();
$(".go").click(function () {
!right
? $(".boat").animate(
{
left: "0",
right: offset.left - 200
},
"veryfast"
)
: $(".boat").animate(
{
left: offset.left - 100,
right: 0
},
"veryfast"
);
right = !right;
});
});
问题是当船回到起始位置时,它会比原来的位置走得更远。
谁能帮帮我?
【问题讨论】:
标签: javascript html jquery css pug