对于长背景图怎么实现滚动条滑动效果?案例代码分享!

听梦的风尘女 2021-08-18 11:12:28 浏览数 (2869)
反馈

在最近小编在做项目的时候遇到了:“对于长背景图怎么实现滚动条滑动效果?”这么个问题,在我完成之后就想着和大家分享有关于实现的方法,希望对大家有所帮助!

最近做项目过程中,老大提了个很奇葩的要求

背景图铺满页面,他要求有滚动条可以滑动,他给我讲的思路是用js 获取背景图片的高,在获取当前窗口的高,两者比较,当窗口的高小于背景图片的高,把窗口的高设置成背景颜色的高。反之就是窗口的高。 感觉太麻烦,用css就可以实现他的要求:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">       
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<title>长背景图测试/title>
<style type="text/css">
        //body里面的属性min-height是关键,网上说直接设置成100vh即可,如果不可以可以自己微调
    body{
        background:url(./images/download_bg.png) no-repeat;
        background-size:100%;
                //方案1
        min-height: 185vh;
                //方案2,本质等价于方案一,如果两个方案是同时开启,则会使用高度更小的那个,已测试
                //height:1200px;
    }
 
    #btn{
        margin-top: 150px;
        text-align: center;
    }  
     
</style>
<body>
 
<div id="btn">
    <a href=" http://www.cnblogs.com/fanbi">
    <img src="./images/download_btn.png" width="90%" alt="跳转到某个网页地址"/></a>
</div>   
 
</body>
</html>

这是网上找的代码。

我改动了下:

body{
    background: -webkit-linear-gradient(to bottom , #699eef, #8e92ef); 
    background: -o-linear-gradient(to bottom , #699eef, #8e92ef); 
    background: -moz-linear-gradient(to bottom , #699eef, #8e92ef); 
    background: linear-gradient(to bottom , #699eef, #8e92ef);
  
}
.app {
    width: 100%;
    /* height:900px; */
    min-height: 120vh;
    /* position: fixed; */
    /* top: 0; */
    /* left: 0; */
    background: url('../../images/bg.jpg') no-repeat;
    background-size: 100% auto;
    
}

给body加了个跟图片颜色一样的背景色

解决问题。

那么以上的话就是有关于:“对于长背景图怎么实现滚动条滑动效果?”这方面的全部内容,更多有关于html5这方面的相关问题和内容我们都可以在W3Cschool进行学习和了解!



0 人点赞