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