【问题标题】:creating an object/class and using it in jquery创建一个对象/类并在 jquery 中使用它
【发布时间】:2010-10-14 13:20:03
【问题描述】:

如果我的问题名称不正确,您好抱歉。

我正在尝试创建一个图像旋转器,但我希望能够在一个页面中多次重复使用它,所以我试图将它创建为一个对象/类。

我在一个页面上有大约 5 或 6 张图片。 我想将每张图片作为一个旋转器,每 2 秒左右显示一张新图片。

    <img src="uploads/Range_Images/p6-7.jpg" class="myclass1"/>
    <img src="uploads/Range_Images/p6-7.jpg" class="myclass2"/>
    <img src="uploads/Range_Images/p6-7.jpg" class="myclass3"/>
    <script type=text/javascript>
       $Rotator1 = new imageRotator1('p12-13.jpg:p18-19.jpg:p4-5.jpg:p8-9.jpg:p6-7.jpg:p10-11.jpg:p14-15.jpg:p16-17.jpg', '.myclass1');
       setInterval($Rotator1.rotateImage(), 1000);

       $Rotator2 = new imageRotator1('p12-13.jpg:p18-19.jpg:p4-5.jpg:p8-9.jpg:p6-7.jpg:p10-11.jpg:p14-15.jpg:p16-17.jpg', '.myclass2');
       setInterval($Rotator2.rotateImage(), 1000);

       $Rotator3 = new imageRotator1('p12-13.jpg:p18-19.jpg:p4-5.jpg:p8-9.jpg:p6-7.jpg:p10-11.jpg:p14-15.jpg:p16-17.jpg', '.myclass3');
       setInterval($Rotator3.rotateImage(), 1000);
    </script>

这就是我用来显示图像和创建我的旋转器类的实例的代码

尽管谷歌搜索了几个小时,但我也能想到一些我不确定答案的问题! 首先这是我的代码(我将 javascript 与 jquery 混合,这可能是我开始出错的地方......

<script type="text/javascript">
   $(document).ready(function(){ // maybe this should not be used when creating a object/class???

   // i want to use this function as a class if possible     
   function imageRotator($images_str, $class){
      // set up the vars
      this.myImg = $images_str; //string containing image names
      this.myClass = $class; //string containing class of image to change
      this.imagelist = this.myImg.split(':'); //split the image string into an array
      this.index = 1; // set the current index count


      // maybe this is where i am going wrong, as in jquery $(this) refers to the current selector and maybe this.myclass (from from the imageRotator object does not work??
      // is it possible to reference a value from an object in jquery?
      function prototype.rotateImage(){
         $(this.myclass).fadeOut('fast', function(){
            $(this).attr('src', this.imagelist[this.index]);
            $(this).fadeIn('fast', function(){
               if (this.index == this.imagelist.length-1){
                  this.index = 0;
               }else{
                  this.index++;
               };
            });
         });
      };
   };
});
</script>

我绝不是编程方面的专家,但我相信这应该以某种方式成为可能。

任何帮助将不胜感激:P

更新:

好的,根据 castrohenges 的回复稍微修改了代码:

<script type="text/javascript">
   $(document).ready(function(){ 
    var imageRotator = function($images_str, $class){
      // set up the vars
      this.myImg = $images_str; //string containing image names
      this.myClass = $class; //string containing class of image to change
      this.imagelist = this.myImg.split(':'); //split the image string into an array
      this.index = 1; // set the current index count
   };
    function imageRotator.prototype.rotateImage(){
        $(this.myclass).fadeOut('fast', function(){
            $(this).attr('src', this.imagelist[this.index]);
            $(this).fadeIn('fast', function(){
                if (this.index == this.imagelist.length-1){
                    this.index = 0;
                }else{
                    this.index++;
                };
            });
        });
    };
});
</script>

</head>

<body>
<img src="uploads/Range_Images/p6-7.jpg" class="myclass1"/>
    <img src="uploads/Range_Images/p6-7.jpg" class="myclass2"/>
    <img src="uploads/Range_Images/p6-7.jpg" class="myclass3"/>
<script type=text/javascript>
    $(document).ready(function(){
             $Rotator1 = new imageRotator('p12-13.jpg:p18-19.jpg:p4-5.jpg:p8-9.jpg:p6-7.jpg:p10-11.jpg:p14-15.jpg:p16-17.jpg', '.myclass1');
             setInterval($Rotator1.rotateImage(), 1000);

             $Rotator2 = new imageRotator('p12-13.jpg:p18-19.jpg:p4-5.jpg:p8-9.jpg:p6-7.jpg:p10-11.jpg:p14-15.jpg:p16-17.jpg', '.myclass2');
             setInterval($Rotator2.rotateImage(), 1000);

             $Rotator3 = new imageRotator('p12-13.jpg:p18-19.jpg:p4-5.jpg:p8-9.jpg:p6-7.jpg:p10-11.jpg:p14-15.jpg:p16-17.jpg', '.myclass3');
             setInterval($Rotator3.rotateImage(), 1000);
        });
</script>
</body>

我遇到了一个错误:第 45 行没有定义图像旋转器

因此将其更改为

var imageRotator = function($images_str, $class){....};

但错误仍然存​​在?

将尝试按照插件指南重新创建此脚本,看看我在哪里......

【问题讨论】:

  • 我觉得你有点困惑。您说“我正在将 javascript 与 jquery 混合,这可能是我开始出错的地方”,但 jQuery is JavaScript.
  • 是的,我知道,jquery 是 jquery 的库,所以如果你使用 $(this).mycommand().fade();单独使用js会报错吗?

标签: javascript jquery oop object class-design


【解决方案1】:

如果你想使用原型,你需要改变

function prototype.rotateImage(){

并将其放在 imageRotator 函数之外。像这样:

function imageRotator($images_str, $class){ ... }

imageRotator.prototype.rotateImage = function() { ... }

或者你也可以看看这个 jQuery 插件http://malsup.com/jquery/cycle/ - 这是我找到的第一个,但我相信还会有更多。

如果您确实想使用自己的自定义代码,我建议将其包装为 jQuery 插件 - http://docs.jquery.com/Plugins/Authoring。这样您就不必担心类架构,所有内容都将很好地封装在 jQuery 对象中。

【讨论】:

  • 非常感谢,我会试一试并报告...我以前看过jquery循环插件,但真的很想学习如何自己编写代码!做是最好的学习方式,我会看看创作指南:)
  • +1。使用或编写 jQuery 插件是正确的方法。
  • 最后我选择了简单的选项,并使用了循环插件,因为它简单地完成了这项工作,虽然我觉得我很容易放弃,明天我会去创造我自己的!感谢人们的帮助。
猜你喜欢
  • 1970-01-01
  • 2013-02-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-14
  • 1970-01-01
  • 1970-01-01
  • 2016-03-29
  • 1970-01-01
相关资源
最近更新 更多