var j$ = jQuery.noConflict();
簡單的jQuery幻燈片腳本 - 我的jQuery語法有什麼問題嗎?
j$(document).ready(function(){
j$.fn.slideShow = function(timeOut){
var $slidecontainer = this;
this.children(':gt(0)').hide();
setInterval(function() {
$slidecontainer.children().eq(0).fadeOut(2000).next().fadeIn(2000).addClass('on').nextAll().removeClass('on').end().appendTo($slidecontainer);}, timeOut || 1000);
var imgheight = this.children('.on').outerHeight();
this.css('height', imgheight);
};
j$(function() {
j$('.slideshow').slideShow(7000);});
});
大部分上面的腳本運行良好。唯一的問題是圖像高度的css沒有被應用到父容器。當我在瀏覽器控制檯中嘗試此功能時,它可以很好地工作當我在頁面中調用腳本時,它不會應用圖像高度。它盡其他一切。
這是HTML
<div id="slideshow" class="slideshow">
<img width="970" height="300" src="someimage.jpg" class="" >
<img width="970" height="300" src="someimage.jpg" class="" >
<img width="970" height="300" src="someimage.jpg" class="" >
<img width="970" height="300" src="someimage.jpg" class="" >
<img width="970" height="300" src="someimage.jpg" class="" >
</div>
下面竟然是答案:
var j$ = jQuery.noConflict();
j$.fn.slideShow = function (timeOut) {
var $slidecontainer = this;
$slidecontainer.children(':gt(0)').hide();
setInterval(function() {
$slidecontainer.children().eq(0).fadeOut(2000).next().fadeIn(2000).addClass('on').nextAll().removeClass('on').end().end().appendTo($slidecontainer);
}, timeOut || 1000);
var imgheight = $slidecontainer.children('img').first().outerHeight();
$slidecontainer.css('height', imgheight);
};
j$(window).load(function() {
j$('.slideshow').slideShow(7000);
});
我會試試這個。謝謝 – greg
沒有工作......仍然沒有將圖像高度應用於父容器 – greg
你最初是否爲任何元素放置了class =「on」?看起來像你沒有,並且你正試圖獲得$('。on')的高度 – sabithpocker