我正在学习JQuery,并且遇到了一个奇怪的问题。我做了一个幻灯片演示,它可以在IE,Firefox 3.0和Firefox 3.5上运行,但最初的图像在Chrome中不起作用。

该脚本只是循环显示图像列表,并根据查看窗口的大小调整div(图像,标题)的大小。如果我移动$(document).ready(function(){});脚本到身体的尽头,脚本工作正常。

我的印象是$(document).ready函数在文档完全加载之前不会被调用。这是否正确,并且Chrome渲染引擎正在做些奇怪的事情,或者我做错了什么?

这里是代码:

slideShow.html

$(document).ready(function() {

slideShow();

});

.

.

.

jquery.slideShow.js

function slideShow() {

//Set the opacity of all images to 0

$('#gallery a').css({ opacity: 0.0 });

//Get the first image and display it (set it to full opacity)

$('#gallery a:first').css({ opacity: 1.0 });

//Set the caption background to semi-transparent

$('#gallery .caption').css({ opacity: 0.7 });

//Resize the width of the caption according to the image width

$('#gallery a:first').find('img').css({ height: $('#gallery a:first').find('img').height() });

$('#gallery a:first').find('img').css({ width: $('#gallery a:first').find('img').width() });

var captionPosition = parseInt($('#gallery a:first').find('img').css('height')) * -1;

if ($(window).height() < $('#gallery a:first').find('img').height()) {

var imageWidth = parseInt($('#gallery a:first').find('img').width());

var imageHeight = parseInt($('#gallery a:first').find('img').height());

$('#gallery a:first').find('img').css({ height: $(window).height() - 10 });

var cssHeight = parseInt($('#gallery a:first').find('img').css('height'));

$('#gallery a:first').find('img').css({ width: parseInt((cssHeight * imageWidth)/cssHeight) });

captionPosition = parseInt($('#gallery a:first').find('img').css('height') * -1);

}

if ($(window).width() < $('#gallery a:first').find('img').width()) {

var imageWidth = parseInt($('#gallery a:first').find('img').width());

var imageHeight = parseInt($('#gallery a:first').find('img').height());

$('#gallery a:first').find('img').css({ width: ($(window).width() - 50) });

var cssWidth = parseInt($('#gallery a:first').find('img').css('width'));

$('#gallery a:first').find('img').css({ height: parseInt((cssWidth * imageHeight)/imageWidth) });

captionPosition = parseInt($('#gallery a:first').find('img').css('height')) * -1;

}

$('#gallery .caption').css({ width: $('#gallery a:first').find('img').css('width') });

$('#gallery .caption').css({ bottom: captionPosition });

//Get the caption of the first image from REL attribute and display it

$('#gallery .content').html($('#gallery a:first').find('img').attr('rel')).animate({ opacity: 0.7 }, 400);

//Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds

setInterval('gallery()', 6000);

}

function gallery() {

//if no IMGs have the show class, grab the first image

var current = ($('#gallery a.show') ? $('#gallery a.show') : $('#gallery a:first'));

//Get next image, if it reached the end of the slideshow, rotate it back to the first image

var next = ((current.next().length) ? ((current.next().hasClass('caption')) ? $('#gallery a:first') : current.next()) : $('#gallery a:first'));

//Get next image caption

var caption = next.find('img').attr('rel');

//Set the fade in effect for the next image, show class has higher z-index

next.css({ opacity: 0.0 }).addClass('show').animate({ opacity: 1.0 }, 1000);

//Hide the current image

current.animate({ opacity: 0.0 }, 1000).removeClass('show');

next.find('img').css({ height: next.find('img').height() });

next.find('img').css({ width: next.find('img').width() });

var captionPosition = parseInt(next.find('img').css('height')) * -1;

if (next.find('img').height() > $(window).height()) {

var imageHeight = parseInt(next.find('img').height());

var imageWidth = parseInt(next.find('img').width());

next.find('img').css({ height: (parseInt($(window).height()) - 50) });

var cssHeight = parseInt(next.find('img').css('height'));

var testVal = parseInt((cssHeight * imageWidth)/imageHeight);

next.find('img').css({ width: testVal });

//alert('css width=' + next.find('img').css('width') + ', css height=' + cssHeight + ', img width = ' + imageWidth + ', img height = ' + imageHeight + ', window width = ' + $(window).width() + ', window height = ' + $(window).height());

captionPosition = parseInt(cssHeight * -1);

}

if (parseInt(next.find('img').css('width')) > parseInt($(window).width())) {

var imageHeight = parseInt(next.find('img').height());

var imageWidth = parseInt(next.find('img').width());

next.find('img').css({ width: (parseInt($(window).width()) - 50) });

var cssWidth = parseInt(next.find('img').css('width'));

var testVal = parseInt((cssWidth * imageHeight)/imageWidth);

next.find('img').css({ height: testVal });

//alert('imageWidth = ' + imageWidth + 'imageHeight = ' + imageHeight + 'css height = ' + next.find('img').css('height') + ', css width = ' + next.find('img').css('width'));

captionPosition = parseInt(next.find('img').css('height')) * -1;

}

$('#gallery .caption').css({ width: next.find('img').css('width') });

$('#gallery .caption').css({ bottom: captionPosition });

//Set the opacity to 0 and height to 1px

$('#gallery .caption').animate({ opacity: 0.0 }, { queue: false, duration: 0 }).animate({ height: '1px' }, { queue: true, duration: 300 });

//Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect

$('#gallery .caption').animate({ opacity: 0.7 }, 100).animate({ height: '100px' }, 500);

//Display the content

$('#gallery .content').html(caption);

}

2009-08-31

JamesL

+0

为了详细说明ceejayoz的回答是:$(文件)则返回包装成一个jQuery对象的DOM - jQuery选择返回某些部分的DOM,包装为一个jQuery对象。正如您现在可能已经了解的那样,DOM并不是页面内容的同义词。 –

2009-08-31 20:09:24

html ready 调用函数,Chrome和JQuery问题 - $(document).ready(function(){});在页面加载之前调用...相关推荐

  1. js 和jQuery(自动执行函数)立即执行函数和页面加载完后执行函数写法

    js 立即执行函数的写法. js 立即执行函数只能用于匿名函数,如果声明了函数名是不可以用立即执行的,通常在函数表达式后加一对小括号()用于立即执行 如果想让函数不被调用的情况下,立即自动执行,需要在 ...

  2. jquery中$(document).ready(function(){//todo});window.onload时间线关系

    1.基于DOM的解析加载过程,即:document.readystate状态, 其有如下四个状态: a.uninitiated,未初始化状态. b.loading,dom开始解析. c.loaded, ...

  3. jQuery的页面加载完毕事件?

    很多时候我们需要获取元素,但是必须等到该元素被加载完成后才能获取.我们可以把js代码放到该元素的后面,但是这样就会造成js在我们的body中存在不好管理.所有页面加载完毕后所有的元素当然已经加载完毕. ...

  4. Jquery中$(document).ready(function(){ })函数的使用详解

    Jquery是优秀的Javascrīpt框架,$是jquery库的申明,它很不稳定(我就常遇上),换一种稳定的写法jQuery.noConflict(); jQuery(document).ready ...

  5. jQuery中$(document).ready()和window.onload的区别

     $(document) ready()和window onload在表面上看都是页面加载时我们就去执行一个函数或动作,但是在具体的细节上$(document) ready()和window onlo ...

  6. 关于使用jQuery时$(document).ready()方法失效问题

    关于使用jQuery时$(document).ready()方法失效问题 <script type="text/javascript">//页面加载后自动执行,不需要按 ...

  7. chromium禁用ajax,页面加载时,jQuery AJAX不会在Chrome / Chromium中启动

    | 场景:我拥有的是一个上传表单,该表单使用nginx uploadProgress模块​​报告上传进度.下面的代码在Firefox中可以正常工作,但是使用Chrome和Chromium时,页面加载时 ...

  8. Jquery中$(document).ready()的作用

    Jquery中$(document).ready()的作用类似于传统JavaScript中的window.onload方法,不过与window.onload方法还是有区别的. 1.执行时间 windo ...

  9. jquery 中 $(document).ready() 与window.onload 的区别

    Jquery中$(document).ready()的作用类似于传统JavaScript中的window.onload方法,不过与window.onload方法还是有区别的. 1.执行时间 windo ...

最新文章

  1. 吃西餐的吴大师略懂《赤壁》
  2. Linux之telnet命令使用详解—网络故障定位(四)
  3. VC++ 中MSDataGrid控件的使用
  4. NYOJ 49 开心的小明
  5. QImage 与 cv::Mat 之间的相互转换
  6. uni-app(从零开始)
  7. 【笔试面试题】腾讯2013实习生面试算法题及参考答案
  8. html5 过渡时间,CSS3 对过渡(transition)进行调速以及延时
  9. oracle 级联外键约束,Oracle 外键的相关约束中级联删除的方案
  10. JSP实用教程(第三版 清华大学出版社)中遇到的问题和解释
  11. FudanNLP学习实例——中文分词部分
  12. 制药企业计算机管理软件,制药企业erp管理系统
  13. OBS bit rate推流比特率最佳设置
  14. 【网站】八大极品桌面壁纸网站,惊艳
  15. 飞秋与虚拟机的有关IP设置
  16. Android开发笔记(一百四十八)自定义输入法软键盘
  17. Codeforces Round #838 (Div. 2)题解
  18. [矩阵的QR分解系列四] QR(正交三角)分解
  19. 使用Python,提取视频文件中的音频
  20. javafx-webview中加载的网页有弹窗不显示问题

热门文章

  1. easyUI +datagirdview加载本地json的方式 笔记
  2. 数据字典模板_巧用单元格保护功能+字典技术,制作高大上的人员信息录入表...
  3. win10开发java_java的开发环境(win10)
  4. mysql in 通配符_mysql必知必会--用通配符进行过滤
  5. gnuplot使用备忘
  6. 如何让vim支持python_无法使vim支持python
  7. mysql查询报错2014_mysql Hibernate 查询时用别名报错
  8. 谷歌开源 Kotlin 版本 gRPC
  9. bootstraptable 怎么在特定行添加数据_同等权限下多任职之间数据权限的实例
  10. 40岁的程序员还能找到工作吗_学会了Vue就能找到前端工作吗