我正在尝试使用jquery的简单动画功能。在我的应用程序中,我有两个按钮“向右滑动”和“向左滑动”。当我们点击这些按钮时,它们会将框向左或向右移动。我的向右移动按钮完美地工作,但是我的向左移动按钮只能工作一次。我的代码有什么问题?以下是我的代码:
$(document).ready(function() {
$("#slideRightButton").click(function() {
$("#boxToBeMoved").animate({
left: '+=10%'
});
});
$("#slideLeftButton").click(function() {
$("#boxToBeMoved").animate({
right: '+=10%'
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<button id="slideRightButton">Slide Right</button>
<button id="slideLeftButton">Slide Left</button>
<p>By default, all HTML elements have a static position, and cannot be moved. To manipulate the position, remember to first set the CSS position property of the element to relative, fixed, or absolute!</p>
<div id="boxToBeMoved" style="background:#98bf21;height:100px;width:100px;position:absolute;"></div>
上面的代码只是 W3Schools 的 jQuery 教程的一个扩展,可以在这里找到:这里