所以我有一個大的JS文件,我從之前的開發人員繼承而來,我試圖讓我的頭部圍繞一些主要部分。這裏是整個代碼:進一步理解javascript init函數的設置
$(function() {
var homepage = (function() {
var startNewsTicker = function() {
var news = $('#newsSlider div'),
idx = 0,
isHovered = false,
TIMEOUT = 6000;
news.mouseenter(function() {
isHovered = true;
});
news.mouseleave(function() {
isHovered = false;
});
setInterval(function() {
if (!isHovered) {
news.eq(idx).fadeOut(500);
idx = (idx + 1 <= news.length - 1) ? idx + 1 : 0;
news.eq(idx).fadeIn(500);
}
}, TIMEOUT);
news.eq(idx).fadeIn(500);
};
var slides = {
wrapper: $('#slideWrapper'),
slide: $('#slideWrapper li'),
imageWrapper: {},
image: {},
paginatorWrapper: {},
paginator: {},
prev: -1,
next: -1,
arrows: {
left: $('#arwLeft'),
right: $('#arwRight')
}
},
centerSlideWrapper = function() {
slides.wrapper.css({
marginTop: ((window.innerHeight - slides.wrapper.height())/2) + 'px',
opacity: 1
});
},
buildSlider = function() {
slides.prev = slides.length - 1;
slides.next = 1;
slides.wrapper.before('<div id="homepageBackgroundImages" />').after('<div id="homepagePagination" />');
slides.imageWrapper = $('#homepageBackgroundImages');
slides.paginatorWrapper = $('#homepagePagination');
slides.paginatorWrapper.css('width', 36 * slides.slide.length + 'px')
slides.slide.each(function() {
var $t = $(this);
slides.imageWrapper.append('<div><img src="' + $t.attr('data-img') + '" /></div>');
slides.paginatorWrapper.append('<a href="#"><span>' + $t.attr('data-tagline') + '</span></a>');
});
slides.image = slides.imageWrapper.find('div');
slides.image.find('img').fullscreenBackground();
slides.paginator = slides.paginatorWrapper.find('a');
slides.imageWrapper.prepend('<div class="mask"></div>');
//create offset margin left to auto center spans
slides.paginator.find('span').each(function() {
var $t = $(this);
$t.css({
top: "-9999px",
display: "block"
});
$t.css({
marginLeft: -(($t.width() + parseFloat($t.css('paddingLeft')) + parseFloat($t.css('paddingRight')))/2) + 2 + 'px',
display: "none",
top: "-56px"
})
});
},
startSlider = function() {
var slideIdx = -1,
currentSlide,
isHovered = false,
interval = {
delay: 8000,
timer: {},
start: function() {
interval.timer = setInterval(function() {
if(!isHovered)
switchSlide(slideIdx + 1);
}, interval.delay);
},
stop: function() {
clearInterval(interval.timer);
}
},
switchSlide = function (idx) {
if (slideIdx > -1) {
slides.image.eq(slideIdx).stop(true, true).fadeOut(500);
slides.slide.eq(slideIdx).stop(true, true).fadeOut(500);
}
idx = (idx <= slides.slide.length - 1) ? idx : 0;
slides.image.eq(idx).stop(true, true).fadeIn(500);
slides.slide.eq(idx).stop(true, true).fadeIn(500);
slideIdx = idx;
slides.paginator.removeClass('current').eq(idx).addClass('current');
slides.prev = (slideIdx - 1 >= 0) ? slideIdx - 1 : slides.slide.length-1;
slides.next = (slideIdx + 1 <= slides.slide.length - 1) ? slideIdx + 1 : 0;
slides.arrows.left.find('span').text(slides.slide.eq(slides.prev).attr('data-tagline'));
slides.arrows.right.find('span').text(slides.slide.eq(slides.next).attr('data-tagline'));
}
slides.paginator.click(function (e) {
e.preventDefault();
interval.stop();
switchSlide($(this).index());
interval.start();
});
slides.arrows.left.click(function (e) {
e.preventDefault();
interval.stop();
switchSlide(slides.prev);
interval.start();
});
slides.arrows.right.click(function (e) {
e.preventDefault();
interval.stop();
switchSlide(slides.next);
interval.start();
});
slides.arrows.left.mouseenter(function (e) {
e.preventDefault();
isHovered = true;
});
slides.arrows.right.mouseenter(function (e) {
e.preventDefault();
isHovered = true;
});
slides.arrows.left.mouseleave(function (e) {
e.preventDefault();
isHovered = false;
});
slides.arrows.right.mouseleave(function (e) {
e.preventDefault();
isHovered = false;
});
slides.slide.find('strong').mouseenter(function() {
isHovered = true;
});
slides.slide.find('strong').mouseleave(function() {
isHovered = false;
});
switchSlide(0);
interval.start();
}
return {
init: function() {
centerSlideWrapper();
$(window).resize(function() {
centerSlideWrapper();
});
buildSlider();
startSlider();
startNewsTicker();
}
}
})();
homepage.init();
});
在這種情況下,我試圖找出是什麼,是打算跟大塊:
var homepage = (function() {
...
return {
init: function() {
centerSlideWrapper();
$(window).resize(function() {
centerSlideWrapper();
});
buildSlider();
startSlider();
startNewsTicker();
}
}
})();
homepage.init();
我感到困惑的是:
1)爲什麼用這種方式創建這個homepage
函數會有好處。
2)此外,我不明白的是函數如何返回一個包含另一個JS函數init
,然後通過使用homepage.init();
運行的對象。這個homepage.init();
調用是否調用homepage
和init
函數來運行?
感謝您的幫助!
非常豐富!感謝您的信息!我一定會研究這個揭示模塊模式。 – 2013-02-28 01:07:29