首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaScript的Autostart函数

JavaScript的Autostart函数
EN

Stack Overflow用户
提问于 2014-07-08 02:16:51
回答 3查看 3K关注 0票数 0

我仍然是JS的初学者,我的一台sites遇到了问题。主页上有一个我想自动启动的滑块。

我一直试图通过查看这个堆栈溢出链接(Autostart jQuery slider)来解决这个问题,但我使用的网站模板上的JS似乎不同。

下面是我正在尝试构建的网站的JavaScript。

代码语言:javascript
复制
(function($) {
window.HomePageSlider = {

    currentSlide: 0,

    init: function() {
        this.container = $("#thb-home-slides");
        this.pictures = $(".thb-home-slide > img");

        this.header = $(".header-container");
        this.footer = $(".home-footer-container");

        this.captions = $(".thb-home-slide-caption");
        this.banners = $(".thb-banner");
        this.homeExpand = $(".thb-home-expand");
        this.controlNext = $(".thb-home-slides-next");
        this.controlPrev = $(".thb-home-slides-prev");
        this.pagerContainer = $(".thb-home-slides-pager");
        this.pager = $(".thb-home-slides-pager a");

        $("body").addClass("thb-loading");

        this.bindEvents();
        this.showHideControls();
        this.loadFrontImage();
    },

    positionElements: function() {
        var $w = $(window),
            header_height = $(".header-container").outerHeight() + ($("#wpadminbar").length ? 28 : 0),
            footer_height = $(".home-footer-container").outerHeight(),
            diff = parseInt( (footer_height - header_height) / 2, 10 );

        if( !footer_height ) {
            footer_height = 48;
        }

        HomePageSlider.captions.css({
            'top' : header_height,
            'bottom' : footer_height
        });

        if( $("html").hasClass("no-csstransforms") ) {
            HomePageSlider.banners.each(function() {
                $(this).css("margin-top", - ($(this).outerHeight() / 2) + diff );
            });
        }
        else {
            HomePageSlider.banners.each(function() {
                $(this).css("margin-top", diff );
            });
        }

        HomePageSlider.pagerContainer.css({
            bottom: footer_height
        });
    },

    loadFrontImage: function() {
        setTimeout(function() {
            if( ! HomePageSlider.pictures.length ) {
                HomePageSlider.container.addClass("thb-slider-loaded");
            }
            else {
                $.thb.loadImage( HomePageSlider.pictures, {
                    imageLoaded: function( image ) {
                        image.parent().thb_stretcher({
                            adapt: false
                        });

                        image.parent().addClass("thb-slide-loaded");
                        $("body").removeClass("thb-loading");

                        setTimeout(function() {
                            HomePageSlider.container.addClass("thb-slider-loaded");
                        }, 10);
                    }
                } );
            }
        }, 500);
    },

    bindEvents: function() {
        $.thb.key("right", function() {
            HomePageSlider.right();
        });

        $.thb.key("left", function() {
            HomePageSlider.left();
        });

        HomePageSlider.controlNext.click(function() {
            HomePageSlider.right();
            return false;
        });

        HomePageSlider.controlPrev.click(function() {
            HomePageSlider.left();
            return false;
        });

        HomePageSlider.homeExpand.click(function() {
            if( $("body").hasClass("w-home-expand") ) {
                $(this).attr("data-icon", "u");
                $("body").removeClass("w-home-expand");
            }
            else {
                $(this).attr("data-icon", "p");
                $("body").addClass("w-home-expand");
            }

            return false;
        });

        HomePageSlider.pager.click(function() {
            if( ! HomePageSlider.container.hasClass("thb-slider-loaded") || thb_moving ) {
                return false;
            }

            var target = $(this).data("target");

            HomePageSlider.pager.removeClass("active");
            $(this).addClass("active");

            if( target !== HomePageSlider.currentSlide ) {
                if( target > HomePageSlider.currentSlide ) {
                    for(i=HomePageSlider.currentSlide; i<target; i++) {
                        HomePageSlider.right(true);
                    }
                }
                else {
                    for(i=HomePageSlider.currentSlide; i>target; i--) {
                        HomePageSlider.left(true);
                    }
                }
            }

            return false;
        });

        $('body.thb-mobile').hammer().bind('swipeleft', function() {
            HomePageSlider.right();
            return false;
        });

        $('body.thb-mobile').hammer().bind('swiperight', function() {
            HomePageSlider.left();
            return false;
        });
    },

    right: function( programmatic ) {
        if( ! programmatic && (! HomePageSlider.container.hasClass("thb-slider-loaded") || thb_moving) ) {
            return false;
        }

        var active_slides = $(".thb-home-slide.active"),
            slides = $(".thb-home-slide"),
            last_active = active_slides.last();

        if( active_slides.length < slides.length ) {
            $.thb.transition(last_active, function() {
                thb_moving = false;
            });

            last_active.addClass("out");
            last_active.next().addClass("active");

            this.currentSlide++;
            thb_moving = true;
        }
        else {
            thb_moving = true;

            $("#thb-home-slides").stop().animate({
                "margin-left": -20
            }, 150, 'linear', function() {
                $(this).stop().animate({
                    "margin-left": 0
                }, 500, 'easeOutElastic', function() {
                    thb_moving = false;
                });
            });
        }

        this.showHideControls();
    },

    left: function( programmatic ) {
        if( ! programmatic && (! HomePageSlider.container.hasClass("thb-slider-loaded") || thb_moving) ) {
            return false;
        }

        var active_slides = $(".thb-home-slide.active"),
            last_active = active_slides.last();

        if( active_slides.length > 1 ) {
            $.thb.transition(last_active, function() {
                thb_moving = false;
            });

            last_active.prev().removeClass("out");
            last_active.removeClass("active");

            this.currentSlide--;
            thb_moving = true;
        }
        else {
            thb_moving = true;

            $("#thb-home-slides").stop().animate({
                "margin-left": 20
            }, 150, 'linear', function() {
                $(this).stop().animate({
                    "margin-left": 0
                }, 500, 'easeOutElastic', function() {
                    thb_moving = false;
                });
            });
        }

        this.showHideControls();
    },

    showHideControls: function() {
        var active_slides = $(".thb-home-slide.active"),
            slides = $(".thb-home-slide");

        HomePageSlider.controlPrev.css({'visibility': 'visible'});
        HomePageSlider.controlNext.css({'visibility': 'visible'});

        if( active_slides.length === 1 ) {
            HomePageSlider.controlPrev.css({'visibility': 'hidden'});
        }

        if( active_slides.length === slides.length ) {
            HomePageSlider.controlNext.css({'visibility': 'hidden'});
        }

        HomePageSlider.pager.removeClass("active");
        HomePageSlider.pager.eq(active_slides.last().index()).addClass("active");
    }
};
EN

回答 3

Stack Overflow用户

发布于 2014-07-08 02:49:41

使用您链接的问题作为参考,我们可以复制autoPlay函数并在init()上调用它;

代码语言:javascript
复制
(function($) {
window.HomePageSlider = {

currentSlide: 0,
timer: null,

init: function() {
    this.container = $("#thb-home-slides");
    this.pictures = $(".thb-home-slide > img");

    this.header = $(".header-container");
    this.footer = $(".home-footer-container");

    this.captions = $(".thb-home-slide-caption");
    this.banners = $(".thb-banner");
    this.homeExpand = $(".thb-home-expand");
    this.controlNext = $(".thb-home-slides-next");
    this.controlPrev = $(".thb-home-slides-prev");
    this.pagerContainer = $(".thb-home-slides-pager");
    this.pager = $(".thb-home-slides-pager a");

    $("body").addClass("thb-loading");

    this.bindEvents();
    this.showHideControls();
    this.loadFrontImage();
    this.autoPlay();
},

positionElements: function() {
    var $w = $(window),
        header_height = $(".header-container").outerHeight() + ($("#wpadminbar").length ? 28 : 0),
        footer_height = $(".home-footer-container").outerHeight(),
        diff = parseInt( (footer_height - header_height) / 2, 10 );

    if( !footer_height ) {
        footer_height = 48;
    }

    HomePageSlider.captions.css({
        'top' : header_height,
        'bottom' : footer_height
    });

    if( $("html").hasClass("no-csstransforms") ) {
        HomePageSlider.banners.each(function() {
            $(this).css("margin-top", - ($(this).outerHeight() / 2) + diff );
        });
    }
    else {
        HomePageSlider.banners.each(function() {
            $(this).css("margin-top", diff );
        });
    }

    HomePageSlider.pagerContainer.css({
        bottom: footer_height
    });
},

loadFrontImage: function() {
    setTimeout(function() {
        if( ! HomePageSlider.pictures.length ) {
            HomePageSlider.container.addClass("thb-slider-loaded");
        }
        else {
            $.thb.loadImage( HomePageSlider.pictures, {
                imageLoaded: function( image ) {
                    image.parent().thb_stretcher({
                        adapt: false
                    });

                    image.parent().addClass("thb-slide-loaded");
                    $("body").removeClass("thb-loading");

                    setTimeout(function() {
                        HomePageSlider.container.addClass("thb-slider-loaded");
                    }, 10);
                }
            } );
        }
    }, 500);
},

bindEvents: function() {
    $.thb.key("right", function() {
        HomePageSlider.right();
    });

    $.thb.key("left", function() {
        HomePageSlider.left();
    });

    HomePageSlider.controlNext.click(function() {
        HomePageSlider.right();
        return false;
    });

    HomePageSlider.controlPrev.click(function() {
        HomePageSlider.left();
        return false;
    });

    HomePageSlider.homeExpand.click(function() {
        if( $("body").hasClass("w-home-expand") ) {
            $(this).attr("data-icon", "u");
            $("body").removeClass("w-home-expand");
        }
        else {
            $(this).attr("data-icon", "p");
            $("body").addClass("w-home-expand");
        }

        return false;
    });

    HomePageSlider.pager.click(function() {
        if( ! HomePageSlider.container.hasClass("thb-slider-loaded") || thb_moving ) {
            return false;
        }

        var target = $(this).data("target");

        HomePageSlider.pager.removeClass("active");
        $(this).addClass("active");

        if( target !== HomePageSlider.currentSlide ) {
            if( target > HomePageSlider.currentSlide ) {
                for(i=HomePageSlider.currentSlide; i<target; i++) {
                    HomePageSlider.right(true);
                }
            }
            else {
                for(i=HomePageSlider.currentSlide; i>target; i--) {
                    HomePageSlider.left(true);
                }
            }
        }

        return false;
    });

    $('body.thb-mobile').hammer().bind('swipeleft', function() {
        HomePageSlider.right();
        return false;
    });

    $('body.thb-mobile').hammer().bind('swiperight', function() {
        HomePageSlider.left();
        return false;
    });
},

right: function( programmatic ) {
    if( ! programmatic && (! HomePageSlider.container.hasClass("thb-slider-loaded") || thb_moving) ) {
        return false;
    }

    var active_slides = $(".thb-home-slide.active"),
        slides = $(".thb-home-slide"),
        last_active = active_slides.last();

    if( active_slides.length < slides.length ) {
        $.thb.transition(last_active, function() {
            thb_moving = false;
        });

        last_active.addClass("out");
        last_active.next().addClass("active");

        this.currentSlide++;
        thb_moving = true;
    }
    else {
        thb_moving = true;

        $("#thb-home-slides").stop().animate({
            "margin-left": -20
        }, 150, 'linear', function() {
            $(this).stop().animate({
                "margin-left": 0
            }, 500, 'easeOutElastic', function() {
                thb_moving = false;
            });
        });
    }

    this.showHideControls();
},

left: function( programmatic ) {
    if( ! programmatic && (! HomePageSlider.container.hasClass("thb-slider-loaded") || thb_moving) ) {
        return false;
    }

    var active_slides = $(".thb-home-slide.active"),
        last_active = active_slides.last();

    if( active_slides.length > 1 ) {
        $.thb.transition(last_active, function() {
            thb_moving = false;
        });

        last_active.prev().removeClass("out");
        last_active.removeClass("active");

        this.currentSlide--;
        thb_moving = true;
    }
    else {
        thb_moving = true;

        $("#thb-home-slides").stop().animate({
            "margin-left": 20
        }, 150, 'linear', function() {
            $(this).stop().animate({
                "margin-left": 0
            }, 500, 'easeOutElastic', function() {
                thb_moving = false;
            });
        });
    }

    this.showHideControls();
},

showHideControls: function() {
    var active_slides = $(".thb-home-slide.active"),
        slides = $(".thb-home-slide");

    HomePageSlider.controlPrev.css({'visibility': 'visible'});
    HomePageSlider.controlNext.css({'visibility': 'visible'});

    if( active_slides.length === 1 ) {
        HomePageSlider.controlPrev.css({'visibility': 'hidden'});
    }

    if( active_slides.length === slides.length ) {
        HomePageSlider.controlNext.css({'visibility': 'hidden'});
    }

    HomePageSlider.pager.removeClass("active");
    HomePageSlider.pager.eq(active_slides.last().index()).addClass("active");
},

autoPlay: function()
{ 
    clearTimeout(this.timer);
    //auto play
    this.timer = setTimeout(function () {
        HomePageSlider.right();
    }, 2500)
}
};
票数 0
EN

Stack Overflow用户

发布于 2017-10-04 00:13:48

只需将其包含在您想要自动启动的函数中即可。另外,将所有脚本放在末尾,就在结束的“body”元素之前。

代码语言:javascript
复制
    (function autorun()
    { 

        yourFunction(); // autostart this one

    })();
票数 0
EN

Stack Overflow用户

发布于 2014-07-08 02:33:11

您可以使用以下两种方法中的任何一种,不过我推荐使用后者。

1)立即调用的函数表达式

代码语言:javascript
复制
(function($) {     

    // function stuff

})(jQuery);

2) jQuery文档就绪:

代码语言:javascript
复制
$(document).ready(function() {

    // function stuff

});

这里的// function stuff应该是window.HomePageSlider = { ... }

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24617104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档