php实现渐变透明,JS实现导航栏的透明渐变

这次给大家带来JS实现导航栏的透明渐变,JS实现导航栏透明渐变的注意事项有哪些,下面就是实战案例,一起来看一下。

mui内置有H5版本的透明渐变导航控件,教程参考mui官网;透明渐变导航是一种解决滚动条通顶的变通方案,相比双webview,具有更高的性能和更好的动画效果;

下面通过本文给大家分享基于原生JS实现 MUI导航栏透明渐变效果,具体内容详情如下所示:

首先声明:由于backgroundColor的值采用的是RGBA,IE8及以下不支持,所以此效果不支持IE8及以下的浏览器

css代码body,p,h1{margin: 0;}

.module-layer{

width: 100%;

position: fixed;

top: 0;

left: 0;

z-index: 100000;

}

.module-layer-content{

position: relative;

min-width: 320px;

max-width: 750px;

width: 100%;

margin: 0 auto;

background-color: rgba(255, 0, 0, 0.9);

}

.module-layer-bg{

width: 100%;

height: 100%;

background: #000;

opacity: .7;

position: absolute;

top: 0;

left: 0;

z-index: -1;

}

.layer-head-name{

height: 50px;

line-height: 50px;

text-align: center;

padding: 0 50px;

font-size: 20px;

}

.layer-return,.layer-share{

width: 50px;

height: 50px;

line-height: 50px;

text-align: center;

position: absolute;

top:0;

z-index: 1;

}

.layer-return{left: 0;}

.layer-share{right: 0;}

.fixed-layer{

height: 100%;

display: none;

z-index: 100001;

}

.relative-layer{height: 100%;}

.layer-content{

padding:3%;

position: relative;

top: 20%;

}

.layer-close{

width: 2rem;

height: 2rem;

position: absolute;

top:3%;

right: 3%;

}

.pr {

position:relative;

}

#shop-input::-webkit-input-placeholder {

color:#fff;

}

#shop-input:-moz-placeholder {

color:#fff;

}

#shop-input::-moz-placeholder {

color:#fff;

}

#shop-input:-ms-input-placeholder {

color:#fff;

}

#shop-input {

border:none;

outline:none;

background:transparent;

}

.search-box {

height:30px;

border-radius:20px;

top:10px;

overflow:hidden;

z-index:10;

}

.search-box:after {

content:'';

display:block;

width:100%;

height:30px;

background:#fff;

opacity:.5;

position:absolute;

top:0;

left:0px;

z-index:-1;

}

#shop-input {

height:28px;

line-height:28px;

font-size:16px;

position:absolute;

top:0;

left:30px;

}

.shop-search {

width:16px;

height:16px;

position:absolute;

top:7px;

left:6px;

}

.layer-return{

background: url(images/return.png) no-repeat center center/12px 20px;

}

.layer-share{

background: url(images/share.png) no-repeat center center/20px 30px;

}

a {

-webkit-tap-highlight-color: transparent;

-webkit-touch-callout: none;

-webkit-user-select: none;

}

.module-content{

min-width: 320px;

max-width: 750px;

width: 100%;

margin: 0 auto;

background: #fff;

}

.module-content p:first-child img{margin-top: 0;}

.module-content p img{

display: block;

width: 100%;

margin-top: 10px;

}

HTML代码

search.png

banner.png

banner1.png

banner3.png

banner4.jpg

banner5.png

banner6.png

banner7.jpg

banner8.jpg

JS代码(function(){

//获取滚动条当前位置

function getScrollTop(){

var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;

if(document.body){

bodyScrollTop = document.body.scrollTop;

}

if(document.documentElement){

documentScrollTop = document.documentElement.scrollTop;

}

scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;

return scrollTop;

}

//获取CSS样式

function getStyle(element, attr){

if(element.currentStyle){

return element.currentStyle[attr];

}else{

return window.getComputedStyle(element,null)[attr];

}

}

//获取原始backgroundColor值

var color = getStyle(document.getElementsByClassName('module-layer-content')[0],'backgroundColor');

//取到RGB

var colorRgb = color.substring(0,color.lastIndexOf(',') + 1);

//取到A

var colorA = color.substring(color.lastIndexOf(',') + 1,color.length - 1);

//对A判断,如果最终值小于0.9,直接设置为1

if(colorA < 0.9){colorA = 1;}

//设置背景色的A的函数

var setCoverOpacity = function() {

document.getElementsByClassName('module-layer-content')[0].style.background = colorRgb + (((getScrollTop() / 550) > colorA) ? colorA : (getScrollTop() / 550)) + ')';

}

//初始化函数

setCoverOpacity();

//绑定滚动监听事件

window.addEventListener('scroll',setCoverOpacity,false);

}())

注意:

不兼容IE8及以下的IE浏览器;

550是滚动条到达位置的最终透明度,此处根据需要自定义;

CSS终背景颜色的RGBA的A是最终透明度

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值