如何使我的插件在重复的HTML标记上工作,但单独操作

How do I make my plug-in work on duplicate HTML markup but operate separately?

本文关键字:工作 操作 单独 HTML 插件 我的 何使      更新时间:2023-09-26

我继承了Javascript和jQuery用于滑动带(转盘)模块。基本上,如果有更多的幻灯片要显示,左导航箭头和右导航箭头会出现在腰带的两侧。

jQuery和Javascript代码最初是一个单例,所以我决定将其转换为jQuery插件,以防belt HTML标记被实例化多次。我们有一个CMS,所以理论上多个皮带模块可以放在同一个模板上。

下面是测试页面,在同一页面上有两个皮带和相关的Javascript jQuery。我的问题是,当你点击箭头或幻灯片时,两条腰带都会做出反应,而不是我与之互动的腰带。

我想做的是,当我将插件连接到DIV时,我希望每条皮带相互独立地运行(即保留自己的局部变量、函数等)。

我不确定我在这里错过了什么。有人能指出我的错误并解释我做错了什么吗?(您应该能够将代码复制并粘贴到jsFiddle中,看看它目前是如何工作的。)

感谢您的阅读。

<html>
   <head>
   <title>Belt Test</title>
   <style type="text/css">
      div.belt { height: 95px; padding: 16px 0 6px 0; position: relative; width: 619px; }
      div.belt span.scrollLeft,
      div.belt span.scrollRight { display: none; padding-top: 30px; text-align: center; width: 48px; }
      div.belt span.scrollLeft { float: left; }
      div.belt span.scrollRight { float: right; }
      div.belt span.scrollLeft a, 
      div.belt span.scrollRight a { background-color: transparent; background-repeat: no-repeat; background-image: url('http://img4.realsimple.com/static/i/gallery-sprite2.gif'); display: block; height: 24px; margin: 0 auto; outline: 0; width: 24px; text-decoration: none; }
      div.belt span.scrollLeft a { background-position: -3px -679px; }
      div.belt span.scrollRight a { background-position: -3px -650px; }
      div.belt span.scrollLeft a:hover { background-position: -3px -737px; text-decoration: none; }
      div.belt span.scrollRight a:hover { background-position: -3px -708px; text-decoration: none; }
      div.belt div.wrapper { height: 87px; overflow: hidden; margin: 0 48px; position: relative; width: 522px; }
      div.belt ul { height:87px; left: 0px; list-style-type: none; margin: 0; padding: 0; position: absolute; top: 0px; }
      div.belt li { display:inline;float:left;padding:6px;}
      div.belt li.selected { border: 1px solid #0099fe; padding: 5px; }
      div.belt li a,
      div.belt li a:hover { outline:none; text-decoration:none; }
      div.belt li img { cursor: pointer; display: block; height: 75px; width: 75px; }
  </style>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
  <script type="text/javascript">
     (function($) {
       /** 
         * @description jQuery Belt plug-in - Add default global belt behavior to ul-style belt where each click on the belt is a page refresh.
         * This is a fixed belt with no customization allowed.  Attach this plug-in to the div encapsulating the UL.
         */
       $.fn.globalbelt = function() {
       /* Inside .each(), this keyword refers to current DOM object */
       return this.each(function() {
            /* We save a reference to the current div element and use it for traversal later */
            var self = this;
            /* Variable initialization */
            var slides = $('ul', self).find('li').size(), /* calc # of slides */
            pages = Math.ceil(slides / 6), /* calc # of pages */
            pageWidth = 522, /* width of one slide page */
            shiftDuration = 400, /* animation duration */
            currentPage = 1, /* current slide page */
            beltSize = slides * 87; /* sets the width of the entire belt */
            $('ul', self).css('width', beltSize + 'px');
            /* hides/shows scrolling arrows */
            setScrollButtons = function(){
                if (currentPage <= 1) {
                    $('.scrollLeft', self).hide();
                } else {
                    $('.scrollLeft', self).show();
                }
                if (currentPage >= pages) {
                    $('.scrollRight', self).hide();
                } else {
                    $('.scrollRight', self).show();
                }
            }
            /* hide / show buttons */
            setScrollButtons();
            /* Button event handler to scroll left */
            $('.scrollLeft a', self).click(function(event){
                currentPage--;
                setScrollButtons();
                $('ul', self).animate({ left: '+=' + pageWidth }, shiftDuration);
                return false;
            });
            /* Button event handler to scroll right */
            $('.scrollRight a', self).click(function(event){
                currentPage++;
                setScrollButtons();
                $('ul', self).animate({ left: '-=' + pageWidth }, shiftDuration);
                return false;
            });
    }); /* return this */
       } /* End plug-in */
 })(jQuery);
  /* Implicitly iterate and bind the plug in to each div that match the selector */
 $(document).ready(function() {
     $('.belt').globalbelt();
 });
</script>
</head>
<body>
<div class="belt">
    <span class="scrollLeft">
        <a href="#">&#160;</a>
    </span>
    <span class="scrollRight">
        <a href="#">&#160;</a>
    </span>
    <div class="wrapper">
        <ul>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
        </ul>
    </div>
</div>

<div class="belt">
    <span class="scrollLeft">
        <a href="#">&#160;</a>
    </span>
    <span class="scrollRight">
        <a href="#">&#160;</a>
    </span>
    <div class="wrapper">
        <ul>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
            <li><a href="#"><img src="http://l.yimg.com/a/p/fi/39/03/54.jpg" title="1" alt="1" border="0" height="75" width="75" /></a></li>
        </ul>
    </div>
</div>
</body>
</html>

这在没有ID的情况下应该是可行的。您只需要在DOM中查找所处的特定div,获取元素对象引用,然后只在DOM的特定子树中执行操作。

因此,在你的点击功能中,你会添加:

$('.scrollLeft a', self).click(function(event){
    var parentDiv = $(this).parents(".belt");
    currentPage--;
    setScrollButtons(parentDiv);
    //...rest of your code...

因此,现在您有了一个对特定皮带元素的引用,该引用将传递给辅助函数。您可以通过以下方式修改您的助手功能:

            setScrollButtons = function(parentDiv){
            if (currentPage <= 1) {
                parentDiv.find('.scrollLeft', self).hide();
            } else {
                parentDiv.find('.scrollLeft', self).show();
            }
            if (currentPage >= pages) {
                parentDiv.find('.scrollRight', self).hide();
            } else {
                parentDiv.find('.scrollRight', self).show();
            }
        }

这样,您仍然只按类进行选择,但您将jQuery从中选择的元素限制在parentDiv的子树中。

除非出现任何JS范围界定问题,否则应该会起作用。如果遇到范围界定问题,可能需要重构代码。

我从未听说过"皮带"这个词。它通常被称为旋转木马。

也就是说,在jQuery中,您可以通过单独选择(通过唯一的ID或CSS3选择器,或者获取所有ID并在jQuery中将其遍历)来实例化每个ID,然后在执行过程中传递每个ID的设置/变量。

所以,不是:

$('.belt').globalbelt();

我建议:

$('#belt1').globalbelt();
$('#belt2').globalbelt();
$('#belt3').globalbelt();

两条皮带都响应的原因是代码绑定到两次点击。

在指定事件处理程序向左滚动的函数中,将目标指定为事件发生的div。当前代码没有该"上下文"信息。

将事件处理程序更改为

$('.scrollLeft a', self).click(function(event){
      self = event.currentTarget.parentElement.parentElement;
      currentPage--;
      setScrollButtons();
      $('ul', self).animate({ left: '+=' + pageWidth }, shiftDuration);
      return false;
});

这将按预期进行。