jquery 在页面加载时向下滑动图像
我对jquery(或java脚本)没有经验,但试图在页面加载时滑入图像(img id=mike
...)。
在浏览了 jquery 文档并尝试了 Google 查询之后,我到目前为止已经想出了这个
$(document).ready(function() {
$("#mike").load(function () {
$(this).show("slide", { direction: "up" }, 2000);
});
});
并应用了 CSS display:hidden
因此它保持隐藏状态,直到我希望它在页面加载时滑入。
除非我重新加载页面,否则这似乎不起作用。
希望有人能帮我解决这个问题:)
I'm not experienced with jquery (or java script really), but trying to make an image (img id=mike
...) slide in when a page loads.
After looking through the jquery docs and trying Google queries, I have so far come up with this
$(document).ready(function() {
$("#mike").load(function () {
$(this).show("slide", { direction: "up" }, 2000);
});
});
And applied the CSS display:hidden
so it remains hidden until I want it to slide in on page load.
This doesn't seem to work unless I reload the page though.
Hoping someone could help me out with this please :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果从缓存中检索图像,则不会触发
.load()
,具体取决于浏览器,因此如果图像完整
,则需要手动触发该事件,例如this:.one()
确保事件仅触发一次。.each()
循环遍历每个元素,其中只有一个如果它已经完整
(如果从缓存中获取的话)会触发该事件以确保它关闭(如果它已经触发,则.one()
小心翼翼地避免滑入两次)。.load()
isn't triggered if the image is retrieves from cache, depending on the browser, so you need to manually fire the event if the image iscomplete
, like this:The
.one()
ensures the event only fires once. The.each()
loops though each element, only one in this case, and if it's alreadycomplete
(which it would be if fetched from cache) fires the event to make sure it goes off (if it already fired, the.one()
took care of it not sliding in twice).@Dean,您可能最好使用 CSS 和 jQuery 的组合来完成这项工作 - 这是为了解决图像的缓存问题(请参阅 Nick 的回复以获取解释)。这是一个粗略的示例:
CSS:
jQuery:
您还可以在页面加载后将动画延迟几秒(或毫秒):
@Dean, you'll probably be best off using a combination of CSS and jQuery to do the job--this is to get around caching issues with images (see Nick's response for an explanation). Here's a rough example:
CSS:
jQuery:
You can also delay the animation a few seconds (or milliseconds) after page load: