我有一个使用大图像作为背景的网页。我希望在下载后使用jQuery加载图像(基本上是bing.com加载其背景图像的方式)。这可能与jQuery?如果是这样,是否有插件,你会推荐?在背景图像中淡入淡出
20
A
回答
11
这article可能是你seful。从那里复制:
HTML
<div id="loader" class="loading"></div>
CSS
DIV#loader {
border: 1px solid #ccc;
width: 500px;
height: 500px;
}
/**
* While we're having the loading class set.
* Removig it, will remove the loading message
*/
DIV#loader.loading {
background: url(images/spinner.gif) no-repeat center center;
}
的Javascript
// when the DOM is ready
$(function() {
var img = new Image();
// wrap our new image in jQuery, then:
$(img)
// once the image has loaded, execute this code
.load(function() {
// set the image hidden by default
$(this).hide();
// with the holding div #loader, apply:
$('#loader')
// remove the loading class (so no background spinner),
.removeClass('loading')
// then insert our image
.append(this);
// fade our image in to create a nice effect
$(this).fadeIn();
})
// if there was an error loading the image, react accordingly
.error(function() {
// notify the user that the image could not be loaded
})
// *finally*, set the src attribute of the new image to our image
.attr('src', 'images/headshot.jpg');
});
25
您可以先加载图像,然后在完成加载后将其设置为背景图像。这样浏览器将(希望)从缓存中加载背景图像,而不是重新加载它。当你要求它作为一个插件:
$.fn.smartBackgroundImage = function(url){
var t = this;
//create an img so the browser will download the image:
$('<img />')
.attr('src', url)
.load(function(){ //attach onload to set background-image
t.each(function(){
$(this).css('backgroundImage', 'url('+url+')');
});
});
return this;
}
使用这样的:
$('body').smartBackgroundImage('http://example.com/image.png');
+6
非常感谢,但我怎样才能使背景图像淡入? – desbest 2012-02-09 19:02:02
1
不能使用CSS背景图像,因为这是不可能的事件附加到图像的加载(我所知道的)。
所以,我给它一个镜头,但没有测试它:
HTML:
<body>
<div class="bgimage"><img src="/bgimage.jpg" ></div>
<div>
... content ...
</div>
</body>
CSS:
.bgimage { position: absolute: }
的Javascript:
$(function() {
$(".bgimage")
.css("opacity",0);
.load(function() { $(this).fadeIn(); });
});
-2
你可以去下面的技巧,对不起,如果它有点脏。
脚本:
jQuery.preloadImages = function() {
for (var i = 0; i < arguments.length; i++) {
jQuery("<img>").attr("src", arguments[i]);
$('.box1').attr('preloadURL', arguments[0]);
}
}
$.preloadImages("sky.jpg");
$(document).ready(function() {
if ($('.box1').attr('preloadURL') == 'sky.jpg') {
$('.box1').css({ 'background-image': 'url(sky.jpg)' },$('.box1').fadeIn(1000));
}
});
标记:
<div class="Content">
<label>Search Bing</label>
<input type="text" />
<input type="button" value="search" />
</div>
<div class="box1"></div>
CSS:
.box1 {background-repeat:no-repeat; width:800px; height:600px; display:none; position:relative;}
.Content { position:absolute; left:20px; top:20px; z-index:100;}
.Content label { color:White;}
希望这有助于
一个小样本上:http://xgreen.co.uk/test.htm
相关问题
- 1. 淡入淡出中的背景图像
- 2. 淡入淡出背景图像淡入淡出div内容
- 3. 淡入淡出背景图像
- 4. 淡入淡出图像到背景
- 5. GWT - 淡入/淡出背景图像
- 6. 背景图像交叉淡入淡出
- 7. 背景图像淡入淡出与jQuery
- 8. 在背景图片中淡入淡出
- 9. 背景淡入淡出
- 10. 在背景中淡入淡出
- 11. 同时淡入淡出背景图片
- 12. 淡入淡出正文背景图片
- 13. JQuery - 淡入淡出新背景图片?
- 14. 在加载背景图像中淡入淡出
- 15. 淡出背景图像
- 16. 在jQuery滑块的背景图像上添加淡入淡出
- 17. jquery - 淡入淡出在悬停的背景图像
- 18. jquery中的背景图像淡入淡出
- 19. jQuery在背景颜色淡入淡出
- 20. 在背景图像顶部淡入淡出幻灯片,作为页面背景
- 21. CSS3的背景图像淡入淡出效果
- 22. 如何使背景图像从灰度淡入淡出颜色?
- 23. 使用jquery连续淡入淡出背景图像
- 24. jQuery使多个背景图像淡入淡出
- 25. jQuery的淡入淡出的CSS背景图像和颜色
- 26. 加载和淡入淡出多个背景图像
- 27. jQuery与背景图像交叉淡入淡出
- 28. jquery淡入淡出渐变背景
- 29. jquery淡入/淡出图像
- 30. 淡入淡出的图像
http://www.techerator.com/2010/11/how-to-make-a-css-background-slideshow-with-jquery/作品 – TFD 2012-08-09 02:05:00