动画渐变(javascript)背景不会延伸到窗口的整个高度
animated gradient (javascript) background will not extend to full height of window
我的网站上有一个动画渐变作为背景div。然而,每当我试图将css样式应用于HEIGHT,如"100%"或"background:cover"等,以便它到达任何窗口的底部时,渐变就会停止工作。它似乎只喜欢采用谨慎的像素高度,这并不理想。下面是我的css和html。关于如何使梯度按比例缩放以适合窗户,而不是只有一个离散的长度,有什么想法吗?我把它设置为宽度:100%,在这个方向上完美发挥作用,所以我被难住了!链接:http://studiopowell.net/TEST_gradient.html
#gradient
{
width: 100%;
height: 1200px;
padding: 0px;
margin: 0px;
opacity: 0.1;
margin-top: -850px;
position: relative;
z-index: 10;
}
</head>
<body>
<div class="titles"><img src="archive-icon.png" width="185" height="185" alt="studio powell michael powell studiopowell art artist books installation video" /><br /><br />M I C H A E L P O W E L L<br /><br /></div>
<a class="fancybox" rel="group" href="archive-icon.png"><img src="ruby ball.jpg" alt="" width="200" /></a>
<div id="gradient">
</div>
</body>
</html>
我相信你可以通过定位文档其他部分上方的梯度div,并使用position:absolute
作为他的评论中提到的@blex来解决你的问题:
http://jsfiddle.net/qj1nz13p/
HTML
<div id="gradient">
</div>
<div class="titles">
<img src="archive-icon.png" width="185" height="185" alt="studio powell michael powell studiopowell art artist books installation video" /><br/><br/>
M I C H A E L P O W E L L<br/><br/>
</div>
<a class="fancybox" rel="group" href="archive-icon.png"><img src="ruby ball.jpg" alt="" width="200" /></a>
CSS
body {
padding:0;
margin:0;
}
#gradient {
width: 100%;
height: 100%;
padding: 0px;
margin: 0px;
opacity: 0.1;
position:absolute;
z-index: -99999;
}
JavaScript
var colors = new Array(
[62,35,255],
[60,255,60],
[255,35,98],
[45,175,230],
[255,0,255],
[255,128,0]);
var step = 0;
//color table indices for:
// current color left
// next color left
// current color right
// next color right
var colorIndices = [0,1,2,3];
//transition speed
var gradientSpeed = 0.0004;
function updateGradient()
{
if ( $===undefined ) return;
var c0_0 = colors[colorIndices[0]];
var c0_1 = colors[colorIndices[1]];
var c1_0 = colors[colorIndices[2]];
var c1_1 = colors[colorIndices[3]];
var istep = 1 - step;
var r1 = Math.round(istep * c0_0[0] + step * c0_1[0]);
var g1 = Math.round(istep * c0_0[1] + step * c0_1[1]);
var b1 = Math.round(istep * c0_0[2] + step * c0_1[2]);
var color1 = "rgb("+r1+","+g1+","+b1+")";
var r2 = Math.round(istep * c1_0[0] + step * c1_1[0]);
var g2 = Math.round(istep * c1_0[1] + step * c1_1[1]);
var b2 = Math.round(istep * c1_0[2] + step * c1_1[2]);
var color2 = "rgb("+r2+","+g2+","+b2+")";
$('#gradient').css({
background: "-webkit-gradient(linear, left top, right top, from("+color1+"), to("+color2+"))"}).css({
background: "-moz-linear-gradient(left, "+color1+" 0%, "+color2+" 100%)"});
step += gradientSpeed;
if ( step >= 1 )
{
step %= 1;
colorIndices[0] = colorIndices[1];
colorIndices[2] = colorIndices[3];
//pick two new target color indices
//do not pick the same as the current one
colorIndices[1] = ( colorIndices[1] + Math.floor( 1 + Math.random() * (colors.length - 1))) % colors.length;
colorIndices[3] = ( colorIndices[3] + Math.floor( 1 + Math.random() * (colors.length - 1))) % colors.length;
}
}
setInterval(updateGradient,10);// JavaScript Document
此外,我将z-index
从10更改为-99999;渐变背景应该是所有东西下面的,对吧?不确定为什么你有10…
将样式应用于HTML标记:http://jsfiddle.net/n6xnsv34/
html{
height:100%;
width: 100%;
background: -webkit-linear-gradient(red, blue); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(red, blue); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(red, blue); /* For Firefox 3.6 to 15 */
background: linear-gradient(red, blue); /* Standard syntax */
}
相关文章:
- 调整缩放窗口高度提升缩放
- javascript skrollr基于动态内容更新窗口高度
- 当移动地址栏出现/消失时,防止更改窗口高度
- 使用JavaScript更改带有窗口高度的元素样式
- 在 ReactJS 中获取视口/窗口高度
- 如何在angular js中获取窗口高度
- 黑莓网站:窗口大小(窗口高度)在黑莓模拟器中返回不正确的值
- 将“窗口高度”指定为“文档高度”
- 使用jQuery如何获取窗口高度
- 始终具有窗口高度的文本区域
- Javascript 函数调整窗口高度和宽度
- 具有 100% 窗口高度的移动导航栏,无论内容如何
- 页面高度等于窗口高度
- 使用 jQuery Event 调整内容窗口高度
- 动态调整 Chrome 扩展程序弹出窗口高度的大小
- 当 #content 区域长于窗口高度(本机或 Vue.js)时收听
- 厚箱或类似的东西,窗口高度为 100%
- 加载 HTML 网站时,安卓上的窗口高度错误
- 每次刷新页面时,JQuery 都会为窗口高度提供不同的值
- 在不使用jQuery的情况下获取移动设备(尤其是iPhone)上的窗口高度