使用HTML或JavaScript移动背景

时间:2011-12-23 12:40:12

标签: javascript html background

我正在尝试创建与下面网站中显示的背景相似的效果。如果可能的话会更喜欢HTML,如果不是,JavaScript应该没问题。谢谢!

Example

1 个答案:

答案 0 :(得分:2)

编辑#2

小提琴:http://jsfiddle.net/nkyt7/3/


修改

小提琴:http://jsfiddle.net/DsRcN/2/


将您网站的内容放在容器DIV或overflow: auto的内容中。并为您的身体元素提供背景图像。在容器的onscroll中,根据需要重新定位主体的背景图像。

CSS:

body {
  background-image: url(bg.png);
  background-position: 0px 0px;
  background-repeat: no-repeat;
}

.container { /* your site's content should be inside this container */
  overflow: auto;
}

JS:

$('.container').scroll(function() {
  $('body').css('background-position', ($(this).scrollTop() * 0.9)+'px');
});

您可以根据您想要的深度效果微调0.9。