简体   繁体   English

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

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

i want to change the box size and how to use animate() method? 我想更改框大小以及如何使用animate()方法? I tried but it's not work... where is the problem?? 我试过但它不起作用......问题出在哪里?

<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>

Here iam using Javascript and HTML to change the box width while click on it 这里我使用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> 

You can use the below code to animate the box on click 您可以使用以下代码在单击时为该框设置动画

<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>

The animate() method is part of a library called jQuery which you need to include using <script></script> tags in your <head></head> to use. animate()方法是名为jQuery的库的一部分,您需要使用<head></head> <script></script>标记来使用它。

In order to use jQuery methods, you need to apply them to a jQuery object. 为了使用jQuery方法,您需要将它们应用于jQuery对象。 Here I have used $('.box') to select elements with the class box . 在这里,我使用$('.box')选择带有类box元素。 In the function, you can then use $(this) (which is a jQuery object) to refer to the box clicked and animate it accordingly. 在函数中,您可以使用$(this) (这是一个jQuery对象)来引用单击的框并相应地为其设置动画。

See example below: 见下面的例子:

 <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