0

我在此处http://www.edhubdemo.com/wp/的一个网站上设置了一个液体轮播滑块。滑块位于我们的作品和我们的客户区域下方,当前用户可以通过单击下一个/上一个按钮滚动浏览作品。现在我想要做的是每 3 秒后应该触发“点击”功能,以便滑块移动到下 4 组作品本身。我尝试使用触发功能,但我想我无法正确集成它。这是液体轮播 javascript 文件中的代码:

(function($){
$.fn.liquidcarousel = function(options) {

var defaults = {
    duration: 10000
};
var options = $.extend(defaults, options);

return this.each(function() {
        var divobj = $(this);

        $(divobj).css('overflow', 'hidden');

        $('> .wrapper', divobj).css('overflow', 'hidden');
        $('> .wrapper', divobj).css('float', 'left');

        $('> .wrapper > ul', divobj).css('float', 'left');
        $('> .wrapper > ul', divobj).css('margin', '0');
        $('> .wrapper > ul', divobj).css('padding', '0');
        $('> .wrapper > ul', divobj).css('display', 'block');

        $('> .wrapper > ul > li', divobj).css('display', 'block');
        $('> .wrapper > ul > li', divobj).css('float', 'left');

        var visiblelis = 0;
        var totallis = $('> .wrapper > ul > li', this).length;
        var currentposition = 0;
        var additionalmargin = 0;
        var totalwidth = 0;

        $(window).resize(function(e){
            var divwidth = $(divobj).width();
            var availablewidth = divwidth;

            var heighest = 0;
            $('> .wrapper > ul > li', divobj).css("height", "auto");
            $('> .wrapper > ul > li', divobj).each(function () {
                if ( $(this).outerHeight() > heighest ) {
                    heighest = $(this).outerHeight();
                }
            });

            $(divobj).height(heighest);
            $('> .wrapper', divobj).height(heighest);
            $('> .wrapper > ul', divobj).height(heighest);
            $('> .wrapper > ul > li', divobj).height(heighest);

            var liwidth = $('> .wrapper > ul > li:first', divobj).outerWidth(true);
            var originalmarginright = parseInt($('> .wrapper > ul > li', divobj).css('marginRight'));
            var originalmarginleft = parseInt($('> .wrapper > ul > li', divobj).css('marginLeft'));
            totalwidth = liwidth + additionalmargin;

            previousvisiblelis = visiblelis;
            visiblelis = Math.floor((availablewidth / liwidth));

            if (visiblelis < totallis) {
                additionalmargin = Math.floor((availablewidth - (visiblelis * liwidth))/visiblelis);
            } else {
                additionalmargin = Math.floor((availablewidth - (totallis * liwidth))/totallis);
            }
            halfadditionalmargin = Math.floor(additionalmargin/2);
            totalwidth = liwidth + additionalmargin;

            if (visiblelis > previousvisiblelis  || totallis <= visiblelis) {
                currentposition -= (visiblelis-previousvisiblelis);
                if (currentposition < 0 || totallis <= visiblelis ) {
                    currentposition = 0;
                }
            }
            $('> .wrapper > ul', divobj).css('marginLeft', -(currentposition * totalwidth));

            if (visiblelis >= totallis || ((divwidth >= (totallis * liwidth)) && options.hidearrows) ) {
                if (options.hidearrows) {
                    $('> .prev', $(divobj).parents(".widget")).hide();
                    $('> .next', $(divobj).parents(".widget")).hide();

                    additionalmargin = Math.floor((divwidth - (totallis * liwidth))/totallis);
                    halfadditionalmargin = Math.floor(additionalmargin/2);
                    totalwidth = liwidth + additionalmargin;
                    $('> .wrapper > ul > li', divobj).css('marginRight', originalmarginright + halfadditionalmargin);
                    $('> .wrapper > ul > li', divobj).css('marginLeft', originalmarginleft + halfadditionalmargin);
                }
                $('> .wrapper', divobj).width(totallis * totalwidth);
                $('> ul', divobj).width(totallis * totalwidth);
                $('> .wrapper', divobj).css('marginLeft', 0);
                currentposition = 0;
            } else {
                $('.prev', $(divobj).parents(".widget")).show();
                $('.next', $(divobj).parents(".widget")).show();
                $('> .wrapper', divobj).width(visiblelis * totalwidth);
                $('> ul', divobj).width(visiblelis * totalwidth);
            }
        });

        $('.next', $(divobj).parents(".widget")).click(function(){

            if (totallis <= visiblelis) {
                currentposition = 0;
            } else if ((currentposition + (visiblelis*2)) < totallis) {
                currentposition += visiblelis;
            } else if ((currentposition + (visiblelis*2)) >= totallis -1) {
                currentposition = totallis - visiblelis;
            }
            $('> .wrapper > ul', divobj).stop();
            $('> .wrapper > ul', divobj).animate({'marginLeft': -(currentposition * totalwidth)}, options.duration);

        $('.prev', $(divobj).parents(".widget")).click(function(){
            if ((currentposition - visiblelis) > 0) {
                currentposition -= visiblelis;
            } else if ((currentposition - (visiblelis*2)) <= 0) {
                currentposition = 0;
            }
            $('> .wrapper > ul', divobj).stop();
            $('> .wrapper > ul', divobj).animate({'marginLeft': -(currentposition * totalwidth)}, options.duration);
        });

        $('.next', $(divobj).parents(".widget")).dblclick(function(e){
            e.preventDefault();
            clearSelection();
        });

        $('.prev', $(divobj).parents(".widget")).dblclick(function(e){
            e.preventDefault();
            clearSelection();
        });

        function clearSelection() {
            if (document.selection && document.selection.empty) {
                document.selection.empty();
            } else if (window.getSelection) {
                var sel = window.getSelection();
                sel.removeAllRanges();
            }
        }

        $(window).resize();
});

这是单击下一个按钮时向前移动滑块的代码部分:

$('.next', $(divobj).parents(".widget")).click(function(){

            if (totallis <= visiblelis) {
                currentposition = 0;
            } else if ((currentposition + (visiblelis*2)) < totallis) {
                currentposition += visiblelis;
            } else if ((currentposition + (visiblelis*2)) >= totallis -1) {
                currentposition = totallis - visiblelis;
            }
            $('> .wrapper > ul', divobj).stop();
            $('> .wrapper > ul', divobj).animate({'marginLeft': -(currentposition * totalwidth)}, options.duration);

这是在单击上一个按钮时使滑块向后移动的代码部分:

$('.prev', $(divobj).parents(".widget")).click(function(){
            if ((currentposition - visiblelis) > 0) {
                currentposition -= visiblelis;
            } else if ((currentposition - (visiblelis*2)) <= 0) {
                currentposition = 0;
            }
            $('> .wrapper > ul', divobj).stop();
            $('> .wrapper > ul', divobj).animate({'marginLeft': -(currentposition * totalwidth)}, options.duration);
        });
4

2 回答 2

0
  • 在 jQuery 中选择你的元素

    var next = $('.next', $(divobj).parents(".widget"));

  • 然后每 3 秒点击一次

    setInterval(next.click, 3000);

然而,最好的方法是为此任务创建一个函数(滑动轮播),然后通过单击调用该函数。然后您可以直接调用该函数,而不是模拟点击。

于 2013-02-27T11:19:32.340 回答
0

.next通过使用模拟鼠标单击事件createEvent("MouseEvents")

$('.next', $(divobj).parents(".widget")).each(function (idx, elm) {
    var ev = document.createEvent("MouseEvents");
    ev.initMouseEvent("click", true, false, self, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    elm.dispatchEvent(ev);
});

我这样做.each是因为一个类是非唯一的,您可能只想为第一个类调用它。

然后可以将其制成每秒钟重复一次的函数3,例如

(function (obj, waitFor) { // wrap to catch variables
    var repeatAction = function repeatAction() { // function to be given timeout (stored in variable
        obj.each(function (idx, elm) { // and named so it can reference itself; makes life easier);
            var ev = document.createEvent("MouseEvents");
            ev.initMouseEvent("click", true, false, self, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
            elm.dispatchEvent(ev);
        });
        window.setTimeout(repeatAction, waitFor); // re-invocation of itself
    };
    window.setTimeout(repeatAction, waitFor); // initial invocation
}($('.next', $(divobj).parents(".widget")), 3000)); // passing vars

编辑:根据要求,将此功能插入到代码中。我还尽可能地修复了语法(OP 的语法中有一些错误)并“美化”了它。

于 2013-02-27T11:17:03.760 回答