每3秒触发一次“点击”事件而不点击下一个按钮?

时间:2013-02-27 11:10:14

标签: javascript jquery slider carousel

我在我的一个网站http://www.edhubdemo.com/wp/上设置了液体旋转木马滑块。滑块位于Our Works和Our Clients区域下方,目前用户可以通过单击下一个/上一个按钮滚动查看作品。现在我想做的是每3秒后应该自动触发'click'功能,以便滑块移动到下一组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);
        });

2 个答案:

答案 0 :(得分:0)

使用createEvent("MouseEvents")

模拟.next上的鼠标点击事件
$('.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

修改:根据要求,this function inserted into the code。我也尽可能地修复语法(OP语法中有一些错误)和“beautified”它。

答案 1 :(得分:0)

  • 在jQuery中选择你的元素

    var next = $(&#39; .next&#39;,$(divobj).parents(&#34; .widget&#34;));

  • 然后每隔3秒点击一次

    setInterval(next.click,3000);

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