PennyWang
PennyWang
  • 发布:2016-12-08 10:34
  • 更新:2017-09-27 11:18
  • 阅读:9237

如何在mui-bar-transparent背景由透明变为不透明时修改导航栏的字体颜色?

分类:MUI
mui

如何在mui-bar-transparent背景由透明变为不透明时修改导航栏的字体颜色?如刚开始导航栏的背景色为透明,字体为白色,在背景透明度为0.5的时候修改<span class="mui-title" >标题</span>字体颜色为黑色?这个该如何解决?

2016-12-08 10:34 负责人:无 分享
已邀请:
PennyWang

PennyWang (作者) - Hbuilder小白一枚,求赐教~~

已解决,在mui.min.js里添加如下代码
if((b.scrollY - this.options.top) / this.options.offset > 0.3){
document.getElementById('bartitle').style.color = 'black';
}else{
document.getElementById('bartitle').style.color = 'white';
}
bartitle为导航栏字体span的id。

mui.min.js里源码如下:
function(a, b) {
var c = /^rgba((\d{1,3}),\s(\d{1,3}),\s(\d{1,3}),\s*(\d{1,3}))$/,
d = function(a) {
var b = a.match(c);
return b && 5 === b.length ? [b[1], b[2], b[3], b[4]] : []
},
e = function(b, c) {
this.element = b, this.options = a.extend({
top: 0,
offset: 150,
duration: 16
}, c || {}), this._style = this.element.style, this._bgColor = this._style.backgroundColor;
var e = d(mui.getStyles(this.element, "backgroundColor"));
if(!e.length) throw new Error("元素背景颜色必须为RGBA");
this._R = e[0], this._G = e[1], this._B = e[2], this._A = e[3], this._bufferFn = a.buffer(this.handleScroll, this.options.duration, this), this.initEvent()
};
e.prototype.initEvent = function() {
b.addEventListener("scroll", this._bufferFn), b.addEventListener(a.EVENT_MOVE, this._bufferFn)
}, e.prototype.handleScroll = function() {
this._style.backgroundColor = "rgba(" + this._R + "," + this._G + "," + this._B + "," + (b.scrollY - this.options.top) / this.options.offset + ")"
if((b.scrollY - this.options.top) / this.options.offset > 0.3){
document.getElementById('bartitle').style.color = 'black';
}else{
document.getElementById('bartitle').style.color = 'white';
}
},

老哥教教我

老哥教教我 - 踏实工作

//字体跟随导航栏标题同时变色!
function toubuyanse() {
var a111 = getComputedStyle(document.getElementById("header"));
var a222 = a111.background;
var a333 = a222.substring(20, 26);
if(a333 > 0.6 || a333 == "one re") { daohanglan121.style.color = "rgb(230,69,74)"; }//daohanglan121是标题栏H1自己加的id
if(a333 < 0.6 || a333 == "0) non") { daohanglan121.style.color = "white"; }

if(a333 == "one re") { document.getElementById("header").style.borderBottom = "1px solid #eee"; } else {  
    document.getElementById("header").style.borderBottom = "0px solid #eee";  
}  

}
setInterval("toubuyanse()", 300);

6***@qq.com

6***@qq.com

Transparent.prototype.handleScroll = function(e) {  
        var y = window.scrollY;  
        if (!this.isNativeScroll && e && e.detail) {  
            y = -e.detail.y;  
        }  
        var opacity = (y - this.options.top) / this.options.offset + this._A;  
        opacity = Math.min(Math.max(this._A, opacity), 1);  
        this._style.backgroundColor = 'rgba(' + this._R + ',' + this._G + ',' + this._B + ',' + opacity + ')';  
        //改变title文字颜色  
        this.element.getElementsByClassName('mui-title')[0].style.color = 'rgba(255,255,255,' + opacity + ')';
x***@happyhuang.com

x***@happyhuang.com

发现一个更好的方法,从API中找到,透明度变化时会触发一个alpha自定义事件
$(".mui-bar-transparent").on("alpha", function(evt) {
var alpha= evt.detail.alpha
//当前获得标题栏透明度,而且是变化时触发
});

爆栈工程师

爆栈工程师 - 爆栈工程师

为什么我的页面滑动的时候没有执行这个方法 也在做这一块 求指导

  • x***@happyhuang.com

    你可以调试mui.js,定位在 Transparent.prototype.handleScroll 函数的执行体里,只要进去就会有 $.trigger(this.element, 'alpha', {

    alpha: opacity

    });这个触发函数,this.element应该跟你的$(".mui-bar-transparent")是一个对象

    2017-06-01 12:17

该问题目前已经被锁定, 无法添加新回复