javascript - 使用动画单击按钮时需要移动到顶部

标签 javascript jquery css

嗨,我想在单击具有动画效果的按钮时移动我的 div,所以我使用 .animate 。但它不能正常工作 这是我正在使用的代码

$(document).ready(function(){
    $("#btn1").click(function(){
        $("#box").animate({margin-Top: "300px"});
    });
   
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btn1">Animate topdown</button>


<div id="box" style="background:#98bf21;height:100px;width:100px;margin:6px;"></div>

我的代码有什么问题我不能使用 top 而不是 margin-top 因为我的 div position:relative;

最佳答案

您需要使用 marginTop 或将属性用引号引起来。

$(document).ready(function() {
  $("#btn1").click(function() {
    $("#box").animate({
      marginTop: "300px"
      // or 
      // "margin-top" : "300px"
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btn1">Animate topdown</button>


<div id="box" style="background:#98bf21;height:100px;width:100px;margin:6px;"></div>

关于javascript - 使用动画单击按钮时需要移动到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40760940/

相关文章:

javascript - 从父级调用 React 组件方法

javascript - 如何通过 JS 代码连接 JS 事件处理程序

javascript - 目标嵌套表格 InDesign 脚本

html - 正确右对齐切换及其标签

javascript - 这段代码有什么问题(响应式 html-css-js)?

javascript - 乘法 2 数组 javascript/jquery

javascript - jQuery 属性选择器问题

javascript - 无法使用 Highchart API 构建多级钻取图表

javascript - 带有CSS的动画搜索栏

javascript - 需要在 CodeIgniter View 中以字符串格式显示一个值