JS实现的动态背景渐变

首先你需要一个放置图片的 div  


<div id="scrollBackground" style="height: 150px; background-image: url(../images/hfbg.jpg);
        background-attachment: initial; background-origin: initial; background-clip: initial;
        background-color: initial; background-position: 0px -1925px; background-repeat: no-repeat no-repeat;">
    </div>

然后需要一段js 设置时间 


<script type="text/javascript">    // hf Background scroll
    window.onload = function () {
        var scrollBackground = {
            object: document.getElementById("scrollBackground"),
            endTop: 3657,
            nowTop: 0
        };
        var scrollInterval = setInterval(function () {
            scrollBackground.nowTop = (scrollBackground.nowTop != scrollBackground.endTop) ? (scrollBackground.nowTop + 1) : 0;
            scrollBackground.object.style.backgroundPosition = "0px -" + scrollBackground.nowTop + "px";
        }, 50);
    }
</script>

然后就搞定了 div 高度可以自己改 无所谓  宽度最大支持1680

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值