javascript - Jquery 插件无法处理多个元素

标签 javascript jquery

感谢您提前提供的帮助。我对 jquery 和插件整体来说还比较陌生。我做了一些研究,并且已经在这个插件上挣扎了至少 3 周。

问题是我试图让多个元素与一个插件一起使用。
插件代码如下

    ;(function($){
$.fn.zoom = function(options){
    //
    var _option = {
        align: "left",              // 
        thumb_image_width: 300,     // 
        thumb_image_height: 400,    // 
        source_image_width: 900,    // 
        source_image_height: 1200,  // 
        zoom_area_width: 600,       // 
        zoom_area_height: "justify",// 
        zoom_area_distance: 10,     // 
        zoom_easing: true,          //
        click_to_zoom: false,
        zoom_element: "auto",
        show_descriptions: true,
        description_location: "bottom",
        description_opacity: 0.7,
        small_thumbs: 3,            //
        smallthumb_inactive_opacity: 0.4,   // 
        smallthumb_hide_single: true,       // 
        smallthumb_select_on_hover: false,
        smallthumbs_position: "bottom",     //
        show_icon: true,
        hide_cursor: false,         // 
        speed: 600,                 // 
        autoplay: true,             // 
        autoplay_interval: 6000,    // 
        keyboard: true,
        right_to_left: false,
    }

    if(options){
        $.extend(_option, options);
    }

    var $ul = $(this);
    if($ul.is("ul") && $ul.children("li").length && $ul.find(".bzoom_big_image").length){

        $ul.addClass('bzoom clearfix').show();
        var $li = $ul.children("li").addClass("bzoom_thumb"),
            li_len = $li.length,
            autoplay = _option.autoplay;
        $li.first().addClass("bzoom_thumb_active").show();
        if(li_len<2){
            autoplay = false;
        }

        $ul.find(".bzoom_thumb_image").css({width:_option.thumb_image_width, height:_option.thumb_image_height}).show();

        var scalex = _option.thumb_image_width / _option.source_image_width,
            scaley = _option.thumb_image_height / _option.source_image_height,
            scxy = _option.thumb_image_width / _option.thumb_image_height;

        var $bzoom_magnifier, $bzoom_magnifier_img, $bzoom_zoom_area, $bzoom_zoom_img;

        // 遮罩显示的区域
        if(!$(".bzoom_magnifier").length){
            $bzoom_magnifier = $('<li class="bzoom_magnifier"><div class=""><img src="" /></div></li>');
            $bzoom_magnifier_img = $bzoom_magnifier.find('img');

            $ul.append($bzoom_magnifier);

            $bzoom_magnifier.css({top:top, left:left});
            $bzoom_magnifier_img.attr('src', $ul.find('.bzoom_thumb_active .bzoom_thumb_image').attr('src')).css({width: _option.thumb_image_width, height: _option.thumb_image_height});
            $bzoom_magnifier.find('div').css({width:_option.thumb_image_width*scalex, height:_option.thumb_image_height*scaley});
        }

        // 大图
        if(!$('.bzoom_zoom_area').length){
            $bzoom_zoom_area = $('<li class="bzoom_zoom_area"><div><img class="bzoom_zoom_img" /></div></li>');
            $bzoom_zoom_img = $bzoom_zoom_area.find('.bzoom_zoom_img');
            var top = 0,
                left = 0;

            $ul.append($bzoom_zoom_area);

            if(_option.align=="left"){
                top = 0;
                left = 0 + _option.thumb_image_width + _option.zoom_area_distance;
            }

            $bzoom_zoom_area.css({top:top, left:left});
            $bzoom_zoom_img.css({width: _option.source_image_width, height: _option.source_image_height});
        }

        var autoPlay = {
            autotime : null,
            isplay : autoplay,

            start : function(){
                if(this.isplay && !this.autotime){
                    this.autotime = setInterval(function(){
                        var index = $ul.find('.bzoom_thumb_active').index();
                        changeLi((index+1)%_option.small_thumbs);
                    }, _option.autoplay_interval);
                }
            },

            stop : function(){
                clearInterval(this.autotime);
                this.autotime = null;
            },

            restart : function(){
                this.stop();
                this.start();
            }
        }

        // 循环小图
        var $small = '';
        if(!$(".bzoom_small_thumbs").length){
            var top = _option.thumb_image_height+10,
                width = _option.thumb_image_width,
                smwidth = (_option.thumb_image_width / _option.small_thumbs) - 10,
                smheight = smwidth / scxy,
                ulwidth = 
                smurl = '',
                html = '';

            for(var i=0; i<_option.small_thumbs; i++){
                smurl = $li.eq(i).find('.bzoom_thumb_image').attr("src");

                if(i==0){
                    html += '<li class="bzoom_smallthumb_active"><img src="'+smurl+'" alt="small" style="width:'+smwidth+'px; height:'+smheight+'px;" /></li>';
                }else{
                    html += '<li style="opacity:0.4;"><img src="'+smurl+'" alt="small" style="width:'+smwidth+'px; height:'+smheight+'px;" /></li>';
                }
            }

            $small = $('<li class="bzoom_small_thumbs" style="top:'+top+'px; width:'+width+'px;"><ul class="clearfix" style="width: 485px;">'+html+'</ul></li>');
            $ul.append($small);

            $small.delegate("li", "click", function(event){
                changeLi($(this).index());
                autoPlay.restart();
            });

            autoPlay.start();
        }

        function changeLi(index){
            $ul.find('.bzoom_thumb_active').removeClass('bzoom_thumb_active').stop().animate({opacity: 0}, _option.speed, function() {
                $(this).hide();
            });
            $small.find('.bzoom_smallthumb_active').removeClass('bzoom_smallthumb_active').stop().animate({opacity: _option.smallthumb_inactive_opacity}, _option.speed);

            $li.eq(index).addClass('bzoom_thumb_active').show().stop().css({opacity: 0}).animate({opacity: 1}, _option.speed);
            $small.find('li:eq('+index+')').addClass('bzoom_smallthumb_active').show().stop().css({opacity: _option.smallthumb_inactive_opacity}).animate({opacity: 1}, _option.speed);

            $bzoom_magnifier_img.attr("src", $li.eq(index).find('.bzoom_thumb_image').attr("src"));
        }




        _option.zoom_area_height = _option.zoom_area_width / scxy;
        $bzoom_zoom_area.find('div').css({width:_option.zoom_area_width, height:_option.zoom_area_height});

        $li.add($bzoom_magnifier).mousemove(function(event){
            var xpos = event.pageX - $ul.offset().left,
                ypos = event.pageY - $ul.offset().top,
                magwidth = _option.thumb_image_width*scalex,
                magheight = _option.thumb_image_height*scalex,
                magx = 0,
                magy = 0,
                bigposx = 0,
                bigposy = 0;

            if(xpos < _option.thumb_image_width/2){
                magx = xpos > magwidth/2 ? xpos-magwidth/2 : 0;
            }else{
                magx = xpos+magwidth/2 > _option.thumb_image_width ? _option.thumb_image_width-magwidth : xpos-magwidth/2;
            }
            if(ypos < _option.thumb_image_height/2){
                magy = ypos > magheight/2 ? ypos-magheight/2 : 0;
            }else{
                magy = ypos+magheight/2 > _option.thumb_image_height ? _option.thumb_image_height-magheight : ypos-magheight/2;
            }

            bigposx = magx / scalex;
            bigposy = magy / scaley;

            $bzoom_magnifier.css({'left':magx, 'top':magy});
            $bzoom_magnifier_img.css({'left':-magx, 'top': -magy});

            $bzoom_zoom_img.css({'left': -bigposx, 'top': -bigposy});
        }).mouseenter(function(event){
            autoPlay.stop();

            $bzoom_zoom_img.attr("src", $(this).find('.bzoom_big_image').attr('src'));
            $bzoom_zoom_area.css({"background-image":"none"}).stop().fadeIn(400);

            $ul.find('.bzoom_thumb_active').stop().animate({'opacity':0.5}, _option.speed*0.7);
            $bzoom_magnifier.stop().animate({'opacity':1}, _option.speed*0.7).show();
        }).mouseleave(function(event){
            $bzoom_zoom_area.stop().fadeOut(400);
            $ul.find('.bzoom_thumb_active').stop().animate({'opacity':1}, _option.speed*0.7);
            $bzoom_magnifier.stop().animate({'opacity':0}, _option.speed*0.7, function(){
                $(this).hide();
            });

            autoPlay.start();
        })
    }
}
    })(jQuery);

要使用插件,我知道我必须像这样触发它

$("#bzoom").zoom({
zoom_area_width: 400,
autoplay_interval :7000,
small_thumbs : 4,
autoplay : true});

但是我想做的是使用多个元素,这样我就可以有两个不同的元素显示这样的图像

$("#bzoom, #bzoom2").zoom({
zoom_area_width: 400,
autoplay_interval :7000,
small_thumbs : 4,
autoplay : true});

这不起作用。它仅适用于一个元素,而不适用于两个元素。我不知道该怎么办。

发生的情况是,每次我包含第二个元素时,第一个元素也会停止工作。好像某个地方发生了冲突。

我的html是这样的

    <div class="bzoom_wrap" style="margin-left:60px">
    <ul id="bzoom"> 
        <li>
            <img class="bzoom_thumb_image" src="../images/500_thumb1.jpg" title="first img" />
            <img class="bzoom_big_image" src="../images/1000_big1.jpg"/>
        </li>
        <li>
    </ul>
</div>


            <div class="bzoom_wrap" style="margin-left:60px">
    <ul id="bzoom2"> 
        <li>
            <img class="bzoom_thumb_image2" src="../images/500_thumb1.jpg" title="first img" />
            <img class="bzoom_big_image2" src="../images/1000_big1.jpg"/>
        </li>
        <li>
    </ul>
</div>

非常感谢任何帮助。

最佳答案

您可以在插件中使用 each 循环:

$.fn.zoom = function(options){
    $(this).each(function(eachElement){
         //code here
    });
}

关于javascript - Jquery 插件无法处理多个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35407969/

相关文章:

javascript - 在 Javascript 中创建类 getter/setter 的最佳方法?

javascript - 动态重新排列 child 以适应有限空间中的大多数

jquery - Materialise slider 在 asp.net mvc 中不起作用

jquery 引用 CSS 类型选择器的问题

javascript - 找不到 Socket.io.js(node.js + express + socket.io)

javascript - timezone.js 在 zend 框架 2 中不工作

javascript - 获取数字之间的字符串内容

javascript - 如何在asp按钮点击中禁用div标签

jquery - 这个 jquery 脚本有什么问题?

javascript - 执行 'post' 时,表中的 Selectmenu 正在发送所有选项而不是选定的选项