繁体   English   中英

我想更改框大小如何使用animate方法?

[英]i want to change the box size how to use animate method?

我想更改框大小以及如何使用animate()方法? 我试过但它不起作用......问题出在哪里?

<style>
.box {
 width: 200px;
 height: 200px;
 background-color: red;
  }
</style>

</head>
<body>
<div class="box"></div>
<script>
 const boxes = document.querySelector(".box");
 boxes.addEventListener("click", boxes1);
  function boxes1() {
    boxes.animate({
      width: "300px"
    });
  }
 /*boxes.click(function(){
  boxes.animate({width:"300"});
   }); << it also not work */
</script>

这里我使用Javascript和HTML来更改框宽,同时单击它

  function change_width() { document.getElementById("box").style.width='300px'; } 
 #box { width: 200px; height: 200px; background-color: red; } 
 <html> <head> </head> <body> <div id="box" onclick="change_width()"></div> </body> </html> 

您可以使用以下代码在单击时为该框设置动画

<html>
<head>
  <style>
    .box {
      width: 200px;
      height: 200px;
      background-color: red;
      transition: width 300ms ease-in;
    }
  </style>
  <title>Title</title>
</head>
<body>
<div class="box"></div>
<script>
  const boxes = document.querySelector(".box");
  boxes.addEventListener("click", boxes1, false);

  function boxes1() {
    this.style.width = "300px";
  }
</script>
</body>
</html>

animate()方法是名为jQuery的库的一部分,您需要使用<head></head> <script></script>标记来使用它。

为了使用jQuery方法,您需要将它们应用于jQuery对象。 在这里,我使用$('.box')选择带有类box元素。 在函数中,您可以使用$(this) (这是一个jQuery对象)来引用单击的框并相应地为其设置动画。

见下面的例子:

 <html> <head> <!-- Include jQuery to use mthods such as .click & .animate --> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <style> .box { width: 200px; height: 200px; background-color: red; } </style> </head> <body> <div class="box"></div> <script> $('.box').click(function(){ $(this).animate({width:"300"}); }); </script> </body> </html> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM