繁体   English   中英

jquery animate()在原型中不起作用

[英]jquery animate() does not work in prototype

如果我调用原型,为什么这个jquery行不起作用?

$("#demo").animate({left: '250px'});

见下文:

这一行:

$("#demo").css("background-color","red");

像我期望的那样工作。

完整代码:

<body>
  <style>
    #demo{width: 400px;}
    button {padding: 10px; background-color: #3f8aca; color: #fff; 
        border-radius: 3px; margin:100px 0 0 100px; border: none;}
  </style>
  <div id="demo"></div>
  <button onclick="porsche.drive();">My Porsche drives.</button>

  <script>
    function Car(){}

    var porsche = new Car(), 
        lancia = new Car();

    Car.fn = Car.prototype;

    Car.fn.drive = function drive(){
      console.log("Yeahh, I´m driving there!");
      document
        .getElementById("demo")
        .innerHTML = "Yeahh, I´m driving there!";
      $("#demo").css("background-color","red");

      // The line below does not work.
      $("#demo").animate({left: '250px'});
    }

    Car.fn.stops = function stops(){
      console.log("Oh, I´ve to stop here!");
    }

    lancia.stops();
  </script>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

</body>

感谢您的建议,Markus

你必须添加position: relative; 像这样 :

#demo {
    width: 400px;
    position: relative;
  }

并添加duration选项:

$("#demo").animate({left: '250px'},{duration:2000});

最终代码:

 <body> <style> #demo{ width: 400px; position: relative; } button {padding: 10px; background-color: #3f8aca; color: #fff; border-radius: 3px; margin:100px 0 0 100px; border: none;} </style> <div id="demo"></div> <button onclick="porsche.drive();">My Porsche drives.</button> <script> function Car(){} var porsche = new Car(), lancia = new Car(); Car.fn = Car.prototype; Car.fn.drive = function drive(){ console.log("Yeahh, I´m driving there!"); document.getElementById("demo").innerHTML = "Yeahh, I´m driving there!"; $("#demo").css("background-color","red"); /* the function below dont works */ $("#demo").animate({left: '250px'},{duration:2000}); } Car.fn.stops = function stops(){ console.log("Oh, I´ve to stop here!"); } lancia.stops(); </script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script> </script> </body> 

 <body> <style> #demo{ width: 400px; position: relative; } button {padding: 10px; background-color: #3f8aca; color: #fff; border-radius: 3px; margin:100px 0 0 100px; border: none;} </style> <div id="demo"></div> <button onclick="porsche.drive();">My Porsche drives.</button> <script> function Car(){} var porsche = new Car(), lancia = new Car(); Car.fn = Car.prototype; Car.fn.drive = function drive(){ console.log("Yeahh, I´m driving there!"); document.getElementById("demo").innerHTML = "Yeahh, I´m driving there!"; $("#demo").css("background-color","red"); /* the function below dont works */ $("#demo").animate({left: '250px'},{duration:2000}); } Car.fn.stops = function stops(){ console.log("Oh, I´ve to stop here!"); } lancia.stops(); </script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script> </script> </body> 

暂无
暂无

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

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