CSS怎么让body背景满屏?不会随着Ctrl+滚轮变小

html-css0117

CSS怎么让body背景满屏?不会随着Ctrl+滚轮变小,第1张

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>无标题文档</title>

<style>

html{width:100%

     height:100%}

body{width:100% 

    height:100%

    background: url(../1.jpg) no-repeat

    background-size:cover     /*这里设置了背景图片为覆盖,以填满整个容器*/

   }

</style>

</head>

<body>

</body>

</html>

如果是在回调里置顶DIV的话(比如点击事件),可以用scrollIntoView。 scrollIntoView如果不想吸顶 可以加个before伪类设置高度 CSS3滚动置顶目前有两种解决方案: 第一种(主流):transform: translate3d(x,y,z) 第二种(未来):position: stickytop:xxx 假设需求: 需求一:滚动过程中A ,C区域不吸顶,B区域吸顶 VUE框架推荐直接使用vantUI插件,indexbar索引栏。https://youzan.github.io/vant/#/zh-CN/index-barvantUI原理用的是transform。 以上面图片为例大致讲解: 1.transform对JS的能力要求高些,页面加载完毕后,给每个B区域对应的DIV设置translate3d的Y轴值,值为B对应div离顶部的高度。 2.当开始滚动起来后,Y轴值为原来值减去滚动条滚动距离值 3.当Y轴值小于0的时候,锁死为0,就自动吸顶了 4.当待置顶DIV的translate3d Y轴值小于DIV的高度时,已置顶DIV的Y轴值开始变为负数,这样会有一个顶出的效果 5.已置顶DIV被待置顶DIV顶出一定距离(一般两倍DIV高度),取消translate3d属性, 6.回滚反向计算即可 需求二:滚动过程中 C不吸顶,B吸顶,A一直吸顶 B在A下面 这个需求vantUI(v2.9.3)目前解决不了,采用translate太复杂,决定采用CSS3新属性position: sticky。 如果是2018年使用该属性兼容性还是个问题,但来到2020年下半年,对大部分生产环境都不是问题。博主测试的IOS11和安卓小米 华为 都支持该属性。博主的建议是:如果是公司APP内嵌H5页面或者微信打开可以使用该属性,如果纯H5页面 ,建议translate. position: sticky字面意思就是粘性定位。 可以粘顶部也可以粘底部 右侧 左侧。 所以除了position: sticky还要给一个定位值,比如top:0px或者right:0px就可以了 非常简单。 比如直接给Bdiv设置下面样式就行 实际使用要注意下面几点: 1.如果想兼容IOS12 IOS11 必须带position: -webkit-sticky而且要在样式表里写,不能写在style里。 2.容器相关。只有要移除容器范畴才起作用。比如body的height不要设置100%,height:100%表示所有元素一直在屏幕范围

<script type="text/javascript">

!function(e,t){var i=e.documentElement,n="orientationchange"in window?"orientationchange":"resize",a=navigator.userAgent.match(/iphone|ipad|ipod/i),d=function(){var e=i.clientWidth,t=i.clientHeighte&&(750<=e&&(e=750),i.style.fontSize=e/750*100+"px",i.dataset.percent=e/750*100,i.dataset.width=e,i.dataset.height=t)}d(),a&&e.documentElement.classList.add("iosx"+t.devicePixelRatio),e.addEventListener&&t.addEventListener(n,d,!1)}(document,window)

</script>

(设计稿尺寸为750)

---  getToken()为定义的方法名称:

安卓 :window.native.方法名()

eg:

  TOKEN = window.native ? window.native.getToken() : ''

  appVersion = window.native ? window.native.getAppVersionName() : ''

  uuid = window.native ? window.native.getUUId() : ''

  phoneModel = window.native ? window.native.getPhoneModel() : ''

  clientId = window.native ? window.native.getClientId() : ''

ios :window.webkit.messageHandlers.方法名.postMessage(null)

eg: 如果没有参数 要写上null

    window.webkit.messageHandlers.getToken.postMessage(null)

    window.webkit.messageHandlers.getClientId.postMessage(null)

    window.webkit.messageHandlers.getPhoneModel.postMessage(null)

    window.webkit.messageHandlers.getUUId.postMessage(null)

    window.webkit.messageHandlers.getAppVersionName.postMessage(null)

--- 引用vconsole.js

<script src="https://cdn.bootcss.com/vConsole/3.3.0/vconsole.min.js"></script>

  <script type="text/javascript">

     var vConsole = new VConsole()

  </script>

(app上可查看log network 相关信息展示)

4.1 --- alert localStorage相关属性失效

查找原因:console.log(window) 打印查看APP里没有相关属性 

解决办法:

寻找app相关人员 加上相关属性。

或者如果有时间原因可以尝试(不一定都适用):

alert换成原生自己写的弹框展示 

localStorage换成全局变量

4.2 ---  使用fullpage.js插件 app里页面空白 浏览器查看页面正常

原因:fullpage.js全屏插件设置html body 高度100%  而APP里面设置高度为依据h5页面高度 导致h5嵌套进去 高度为0 页面自然空白 

解决办法: 

1:app修改设置 参考回答: https://stackoverflow.com/questions/32729416/html-height-100-ignored-by-webview  

2:h5 初始化插件之后 再设置html body高度为100%, body等元素背景图片 

eg:

$('#fullPage').fullpage({}) 

$('html').css('height',window.innerHeight)

$('html').css('width',window.innerWidth)

$('html').css({'background':'url(https://zhuoyou-shop.oss-cn-hangzhou.aliyuncs.com/static/lottery/img/bk.jpg) no-repeat center','background-size':'cover'})

$('body').css('height',window.innerHeight)

$('body').css('width',window.innerWidth)

--- 页面在向下滚动的时候,有些元素会产生细小的动画效果。虽然动画比较小,但却能吸引浏览者的注意,使页面更吸人眼球。

--- WOW.js 依赖animate.css,所以它支持 animate.css 多种的动画效果,能满足各种需求。

--- IE6、IE7 等老旧浏览器不支持 CSS3 动画,所以没有效果;而 wow.js 也使用了 querySelectorAll 方法,IE 低版本会报错。为了达到更好的兼容,最好加一个浏览器及版本判断。

使用方法:

1、引入文件

<link rel="stylesheet" href="css/animate.min.css">

2、HTML

<div class="wow slideInLeft"></div>

<div class="wow slideInRight"></div>

可以加入 data-wow-duration(动画持续时间)和 data-wow-delay(动画延迟时间)属性,如:

<div class="wow slideInLeft" data-wow-duration="2s" data-wow-delay="5s"></div>

<div class="wow slideInRight" data-wow-offset="10"  data-wow-iteration="10"></div>

3、JavaScript

new WOW().init()

如果需要自定义配置,可如下使用:

var wow = new WOW({

    boxClass: 'wow', //‘wow’需要执行动画的元素的 class

    animateClass: 'animated', //‘animated’animation.css 动画的 class

    offset: 0, //0距离可视区域多少开始执行动画

    mobile: true, //true是否在移动设备上执行动画

    live: true //true异步加载的内容是否有效

})

wow.init()

一个 demo 尝试:

是要引入animate.css和wow.js的

html :

<ul class="list2">

<li class="wow fadeInLeft animated"></li>

<li class="wow fadeInRight animated" data-wow-delay="0.3s"></li>

<li class="wow fadeInLeft animated" data-wow-delay="0.6s"></li>

<li class="wow fadeInRight animated" data-wow-delay="0.9s"></li>

</ul>

<ul class="ft-service">

<li class="wow fadeInUpBig animated"></li>

<li class="wow fadeInUpBig animated" data-wow-delay="0.3s"></li>

<li class="wow fadeInUpBig animated" data-wow-delay="0.6s"></li>

<li class="wow fadeInUpBig animated" data-wow-delay="0.9s"></li>

<li class="wow fadeInUpBig animated" data-wow-delay="1.2s"></li>

</ul>

css :

ul{

width: 1200px

overflow: hidden

}

.list2{

width: 760px

display: flex

justify-content: space-between

flex-wrap: wrap

}

.list2 .wow{

display: inline-block

    width: 50%

    height: 560px

background-image: url(https://cdn.dowebok.com/131/images/i2/list2-5.jpg)

}

.ft-service .wow {

    display: inline-block

    width: 236px

    height: 125px

    background-image: url(https://cdn.dowebok.com/131/images/i2/ft-service.png)

    background-repeat: no-repeat

}

js :

<script type="text/javascript">

var wow = new WOW({

    boxClass: 'wow',

    animateClass: 'animated', 

    offset: 0,

    mobile: true,

    live: true

})

wow.init()

</script>

这样就有了一个可以自己发挥的动画效果了。

就是一个比较方便的进入 或者 滚动 的时候的页面的加载动画了,可以随意加入自己想要的动画。

animate.css也可以与别的插件结合使用 比如 fullpage.js都是可以有这种页面进入 滚动时候加入动画的效果。

fullpage.js 与 animate.css 实现的动画效果:

demo :

也是要引入相关文件 fullpage.js  fullpage.css  animate.css

HTML :

<div id="fullPage">

  <div class="section">

    <h3 id="p1" class="display">第一屏</h3>

  </div>

  <div class="section">

  <h3 id="p2" class="display" onclick="getNext()">向上滑动</h3>

  </div>

  <div class="section">

    <h3 id="p3" class="display">第三屏</h3>

  </div>

  <div class="section">

    <h3 id="p4" class="display">第四屏</h3>

    <p id="p5" class="display">这是最后一屏</p>

    <p id="p6" class="display" onclick="getTop()">返回首屏</p>

  </div>

</div>

css :

.section { text-align: centerfont: 50px "Microsoft Yahei"color: #fff}

.section p{font: 30px "Microsoft Yahei"}

.display{display: none}

.display1{display: block}

JS :

<script>

$(function(){

$('#fullPage').fullpage({

sectionsColor: ['#1bbc9b', '#4BBFC3', '#7BAABE', '#f90'],

//滚动到某一屏后

afterLoad: function(anchorLink, index){

if(index == 1){ 

        $('#p1').addClass('animate__animated animate__bounceInLeft display1')

}

if(index == 2){ 

        $('#p2').addClass('animate__animated animate__bounceInLeft display1')

}

if(index == 3){ 

        $('#p3').addClass('animate__animated animate__bounceInLeft display1')

}

if(index == 4){ 

        $('#p4').addClass('animate__animated animate__bounceInLeft display1')

        $('#p5').addClass('animate__animated animate__bounceInLeft animate__delay-1s display1')

        $('#p6').addClass('animate__animated animate__bounceInLeft animate__delay-2s display1')

}

},

onLeave: function(index, direction){

      if(index == '1'){ 

        $('#p1').removeClass('animate__animated animate__bounceInLeft display1')

}

if(index == '2'){ 

        $('#p2').removeClass('animate__animated animate__bounceInLeft display1')

}

if(index == '3'){ 

        $('#p3').removeClass('animate__animated animate__bounceInLeft display1')

}

if(index == '4'){ 

        $('#p4').removeClass('animate__animated animate__bounceInLeft display1')

        $('#p5').removeClass('animate__animated animate__bounceInLeft animate__delay-1s display1')

        $('#p6').removeClass('animate__animated animate__bounceInLeft animate__delay-2s display1')

}

}

})

})

//跳转到某一屏

function getNext(){

$.fn.fullpage.moveTo(3)

}

//没有动画的跳转到首屏

function getTop(){

$.fn.fullpage.silentMoveTo(1)

}

</script>

这样的话 也有和wow.js类似的效果

都是页面进入 和 滚动时候 给元素加入动画效果。