【发布时间】:2012-08-25 18:30:32
【问题描述】:
var j$ = jQuery.noConflict();
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);
});
【问题讨论】: