h5页面背景图很长要有滚动条滑动效果实现代码

作者:袖梨 2022-06-25

本篇文章小编给大家分享一下h5页面背景图很长要有滚动条滑动效果实现代码,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

背景图铺满页面,要求有滚动条可以滑动,思路是用js获取背景图片的高,在获取当前窗口的高,两者比较,当窗口的高小于背景图片的高,把窗口的高设置成背景颜色的高。反之就是窗口的高。



       




长背景图测试/title>
<style type="text/<a href="http://www.111com.net/cssdiv/css.html" target="_blank">css</a>">
        //body里面的属性min-height是关键,网上说直接设置成100vh即可,如果不可以可以自己微调
    body{
        background:url(./images/download_bg.png) no-repeat;
        background-size:100%;
                //方案1
        min-height: 185vh;
                //方案2,本质等价于方案一,如果两个方案是同时开启,则会使用高度更小的那个,已测试
                //
    }
 
    #btn{
        margin-top: 150px;
        text-align: center;
    }  
     
</style>
<body>
 
<div id="btn">
    <a href=" http://www.cnblogs.com/fanbi">
    <a href="https://img.111cn.net./images/download_btn.png" class="js-smartPhoto-pc" target="_blank"><img src="https://img.111cn.net./images/download_btn.png" width="90%" alt="跳转到某个网页地址"/></a></a>
</div>   
 
</body>
</html></pre>
<p>
	这是网上的代码。
</p>
<p>
	改动了下
</p>
<pre class="brush:xhtml;">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%;
    /*  */
    min-height: 120vh;
    /* position: fixed; */
    /* top: 0; */
    /* left: 0; */
    background: url('../../images/bg.jpg') no-repeat;
    background-size: 100% auto;
    
}</pre>
<p>
	给body加了个跟图片颜色一样的背景色
</p>                            </div>
        </div>
            </section>
    <section class="wrap-box">
        <div class="g-tit">
            <h2>相关文章</h2>
        </div>
        <ul class="s-list nobord notop">
                            <li>
                    <a href="/art-422484.htm" class="s-card">
                        <div class="s-card-l">
                            <p class="tit">“十月朝,糍粑碌碌烧”说的是小雪时节的哪一项习俗  蚂蚁庄园11月22日答案早知道</p>
                            <div class="info">
                                <span class="person">游戏攻略</span>
                                <span class="time">2024-11-25</span>
                            </div>
                        </div>
                        <div class="s-card-pic">
                            <img src="/images/lazy.gif" data-src="/uploads/20241125/logo_67445b7e892e41.png" alt="“十月朝,糍粑碌碌烧”说的是小雪时节的哪一项习俗  蚂蚁庄园11月22日答案早知道" />
                        </div>
                    </a>
                </li>
                            <li>
                    <a href="/art-422483.htm" class="s-card">
                        <div class="s-card-l">
                            <p class="tit">以闪亮之名宠物礼包怎么样</p>
                            <div class="info">
                                <span class="person">游戏攻略</span>
                                <span class="time">2024-11-25</span>
                            </div>
                        </div>
                        <div class="s-card-pic">
                            <img src="/images/lazy.gif" data-src="/uploads/20241125/logo_67444aa6a570b1.jpeg" alt="以闪亮之名宠物礼包怎么样" />
                        </div>
                    </a>
                </li>
                            <li>
                    <a href="/art-422482.htm" class="s-card">
                        <div class="s-card-l">
                            <p class="tit">崩坏星穹铁道星期日用什么光锥</p>
                            <div class="info">
                                <span class="person">游戏攻略</span>
                                <span class="time">2024-11-25</span>
                            </div>
                        </div>
                        <div class="s-card-pic">
                            <img src="/images/lazy.gif" data-src="/uploads/20241125/logo_67444aa5cd6c41.jpeg" alt="崩坏星穹铁道星期日用什么光锥" />
                        </div>
                    </a>
                </li>
                            <li>
                    <a href="/art-422480.htm" class="s-card">
                        <div class="s-card-l">
                            <p class="tit">崩坏星穹铁道星期日用什么光锥 崩铁星期日光锥推荐搭配介绍</p>
                            <div class="info">
                                <span class="person">游戏攻略</span>
                                <span class="time">2024-11-25</span>
                            </div>
                        </div>
                        <div class="s-card-pic">
                            <img src="/images/lazy.gif" data-src="/uploads/20241125/logo_6744442f826b91.jpeg" alt="崩坏星穹铁道星期日用什么光锥 崩铁星期日光锥推荐搭配介绍" />
                        </div>
                    </a>
                </li>
                            <li>
                    <a href="/art-422481.htm" class="s-card">
                        <div class="s-card-l">
                            <p class="tit">崩坏星穹铁道星期日技能机制怎么样 崩铁星期日技能机制介绍</p>
                            <div class="info">
                                <span class="person">游戏攻略</span>
                                <span class="time">2024-11-25</span>
                            </div>
                        </div>
                        <div class="s-card-pic">
                            <img src="/images/lazy.gif" data-src="/uploads/20241125/logo_674444301a6b21.jpeg" alt="崩坏星穹铁道星期日技能机制怎么样 崩铁星期日技能机制介绍" />
                        </div>
                    </a>
                </li>
                            <li>
                    <a href="/art-422479.htm" class="s-card">
                        <div class="s-card-l">
                            <p class="tit">崩坏星穹铁道星期日遗器怎么选择 崩铁星期日遗器推荐搭配介绍</p>
                            <div class="info">
                                <span class="person">游戏攻略</span>
                                <span class="time">2024-11-25</span>
                            </div>
                        </div>
                        <div class="s-card-pic">
                            <img src="/images/lazy.gif" data-src="/uploads/20241125/logo_6744442e529711.jpeg" alt="崩坏星穹铁道星期日遗器怎么选择 崩铁星期日遗器推荐搭配介绍" />
                        </div>
                    </a>
                </li>
                    </ul>
    </section>
    <section class="wrap-box">
        <div class="g-tit">
            <h2>精彩推荐</h2>
        </div>
        <ul class="card-box">
                            <li class="card3">
                    <a href="/app/99428.htm" target="_self" class="figure">
                        <div class="figure-box">
                            <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241018/671258fc7c56f.png" alt="沙威玛传奇安卓版" />
                        </div>
                        <p class="figure-head">沙威玛传奇安卓版</p>
                        <span class="figure-btn">下载</span>
                    </a>
                </li>
                            <li class="card3">
                    <a href="/app/99427.htm" target="_self" class="figure">
                        <div class="figure-box">
                            <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241018/6711fc29100e3.png" alt="战盟大乱斗最新版" />
                        </div>
                        <p class="figure-head">战盟大乱斗最新版</p>
                        <span class="figure-btn">下载</span>
                    </a>
                </li>
                            <li class="card3">
                    <a href="/app/99426.htm" target="_self" class="figure">
                        <div class="figure-box">
                            <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241018/6711ef93e4d90.png" alt="大侠立志传最新版" />
                        </div>
                        <p class="figure-head">大侠立志传最新版</p>
                        <span class="figure-btn">下载</span>
                    </a>
                </li>
                            <li class="card3">
                    <a href="/app/99425.htm" target="_self" class="figure">
                        <div class="figure-box">
                            <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241018/6711ca5ba8572.png" alt="苍翼混沌效应最新版" />
                        </div>
                        <p class="figure-head">苍翼混沌效应最新版</p>
                        <span class="figure-btn">下载</span>
                    </a>
                </li>
                    </ul>
        <ul class="card-box-b">
                            <li class="card10">
                    <a href="/app/99454.htm" target="_self" class="figure2">
                        <div class="figure-box">
                            <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241019/671393f16002e.jpg" alt="驯龙高手旅程安卓版" />
                        </div>
                        <div class="figure-cont">
                            <p class="figure-head">驯龙高手旅程安卓版</p>
                            <div class="figure-desc">
                                <span>模拟经营</span>
                                <span>驯龙高手旅程安卓版</span>
                            </div>
                            <div class="figure-desc">
                                <p>一款以奇幻世界为背景的模拟经营游戏</p>
                            </div>
                        </div>
                        <span class="figure-btn">下载</span>
                    </a>
                </li>
                            <li class="card10">
                    <a href="/app/99397.htm" target="_self" class="figure2">
                        <div class="figure-box">
                            <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241015/670dc626d93dd.png" alt="全民农场最新版" />
                        </div>
                        <div class="figure-cont">
                            <p class="figure-head">全民农场最新版</p>
                            <div class="figure-desc">
                                <span>模拟经营</span>
                                <span>全民农场最新版</span>
                            </div>
                            <div class="figure-desc">
                                <p>一款轻松风格的农场经营类型的游戏</p>
                            </div>
                        </div>
                        <span class="figure-btn">下载</span>
                    </a>
                </li>
                            <li class="card10">
                    <a href="/app/99010.htm" target="_self" class="figure2">
                        <div class="figure-box">
                            <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20240907/logo_66dc0ba5ab7021.jpg" alt="散修功德模拟器完整版" />
                        </div>
                        <div class="figure-cont">
                            <p class="figure-head">散修功德模拟器完整版</p>
                            <div class="figure-desc">
                                <span>模拟经营</span>
                                <span>散修功德模拟器完整版</span>
                            </div>
                            <div class="figure-desc">
                                <p>散修功德模拟器游戏是一款以功德修炼为题材的模拟游戏</p>
                            </div>
                        </div>
                        <span class="figure-btn">下载</span>
                    </a>
                </li>
                            <li class="card10">
                    <a href="/app/99001.htm" target="_self" class="figure2">
                        <div class="figure-box">
                            <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20240907/logo_66dc094ae30941.jpg" alt="我的毕业日记无限金币下载" />
                        </div>
                        <div class="figure-cont">
                            <p class="figure-head">我的毕业日记无限金币下载</p>
                            <div class="figure-desc">
                                <span>模拟经营</span>
                                <span>我的毕业日记无限金币下载</span>
                            </div>
                            <div class="figure-desc">
                                <p>开始毕业后的新生活吧!</p>
                            </div>
                        </div>
                        <span class="figure-btn">下载</span>
                    </a>
                </li>
                            <li class="card10">
                    <a href="/app/98996.htm" target="_self" class="figure2">
                        <div class="figure-box">
                            <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20240907/logo_66dbd35fadafd1.png" alt="城堡之梦拼图和装饰下载最新版" />
                        </div>
                        <div class="figure-cont">
                            <p class="figure-head">城堡之梦拼图和装饰下载最新版</p>
                            <div class="figure-desc">
                                <span>模拟经营</span>
                                <span>城堡之梦拼图和装饰下载最新版</span>
                            </div>
                            <div class="figure-desc">
                                <p>城堡之梦拼图和装饰玩家将会在游戏当中感受更多的乐趣体验,你可</p>
                            </div>
                        </div>
                        <span class="figure-btn">下载</span>
                    </a>
                </li>
                    </ul>
    </section>
    <footer class="foot">
    <a href="/" class="logo-icon">
        <img src="/mobile/images/logo2.png" alt="一聚教程网">
    </a>
    <p>Copyright © 2010-2022</p>
    <p>111cn.net All Rights Reserved</p>

</footer>
<script>
    var advData = {"img_fixed_pc_adv":"https:\/\/img.111cn.net\/uploads\/20240509\/663c2e9729f58.jpg","img_fixed_mob_adv":"https:\/\/img.111cn.net\/uploads\/20240509\/663c2e8793225.jpg","url_adv":"http:\/\/shop.hushen.cn\/shop\/c\/baojianpin.html","str_adv":"\u864e\u795e\u5546\u57ce\uff1a\u5173\u7231\u7537\u6027\uff0c\u66f4\u61c2\u7537\u4eba\u3002\u89e3\u51b3\u5927\u4f17\u7684\u7537\u8a00\u4e4b\u9690","img_popup_adv":"https:\/\/img.111cn.net\/uploads\/20240509\/663c2e748238d.png","pc_show_img":"2","pc_show_popup":"2","pc_show_video":"2","mob_show_img":"2","mob_show_popup":"2","mob_show_video":"2","close_adv":"https:\/\/img.111cn.net\/uploads\/20240508\/663b20650801e.png","video_adv":"\/pc\/images\/pc-adv.mp4"};
</script>
<script src="/jspc/funcmob.js" type="text/javascript"></script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-DSRRGRV1TL"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-DSRRGRV1TL');
</script>    <div class="back-top" style="display: block;">
        <span class="icon-box">
            <svg class="icon" viewBox="0 0 1024 1024">
                <path d="M213.333333 640h170.666667v256h256v-256h170.666667l-298.666667-341.333333zM170.666667 128h682.666666v85.333333H170.666667z" fill="#0374f3"></path>
            </svg>
        </span>
    </div>
</div>
<script src="/js/stat.js"></script>
</body>
</html>