騰訊DeepOcean原創文章:dopro.io/egame-weex-…css
隨着電競業務的不斷髮展,頁面功能愈來愈多,交互邏輯更加複雜,相似無限滾動、上拉刷新、橫豎切換滾動等形式在業務中已經是標配,通過重重優化後在H5中的體驗一直達不到理想狀態,沒錯,種種卡,H5的性能太差! 是持續優化仍是破然後立選擇新的技術方向呢?咱們選擇了更有效的後者。
H5 | Weex | Native | |
---|---|---|---|
開發成本 | 低 | 中 | 高 |
維護更新 | 簡單 | 簡單 | 複雜 |
用戶體驗 | 差 | 優 | 優 |
發版審覈 | 不須要 | 不須要 | 須要 |
跨平臺性 | 優 | 優 | 差 |
H5與Weex的區別html
下圖是電競重構稿H5與weex目錄結構對比,以前H5開發是基於jinja模版,採用grunt構建,在release中生成相應的html文件,而weex則主要在src中開發組件,採用webpack編譯,最終會在dist中生成相對應地web和weex版jsbundle文件,再由weex.html生成的二維碼查看weex版頁面效果。前端
此外weex下的src目錄內容是與開發側保持一致的,這樣的好處在於開發人員只須要關注組件的結構變化,其它資源直接更新替換便可。vue
二、在build文件夾下webpack.base.conf.js的rules裏面添加配置node
rules:{
{
test: /\.(scss|css)$/,
loader: 'vue-style-loader!css-loader!sass-loader!stylus-loader?indentedSyntax',
}
}er-width:$border.width;
}
//variable.scss
$border={
color: #E9E9E9
width:2px
}
@import "./variable.scss";
.border{
border-color:$border.color;
border-width:$border.width;
}
web: ios、android: android
二、不支持transform:skew 對於這一類角標須要作傾斜處理能夠採用 圖片加 漸變代碼處理webpack
二、避免在image標籤上使用v-for,不然會致使安卓上圖片渲染異常(如slider中的圖片)ios
注意:box-shadow (自己不支持android),background-image不支持IOS透明css3
屬性 | IOS | Android | H5 |
---|---|---|---|
color | 支持 | 支持 | 支持 |
opacity | 支持 | 支持 | 支持 |
border-color | 支持 | 支持 | 支持 |
box-shadow | 支持 | 不支持 | 支持 |
background-color | 支持 | 支持 | 支持 |
background-image | 不支持 | 支持 | 支持 |
<template>
<div class="ui-btn">
<text class="ui-btn-text">下載</text>
</div>
</template>
<style scoped>
.ui-btn{
opacity: 1; /*必須添加*/
}
.ui-btn:active{
opacity: .5;
}
</style>
<template>
<text class="info-text"
@click="textClick"
:style="textStyle">城市賽戰報,《王者榮耀》城市賽鄭州站歡樂落幕城市賽戰報,《王者榮耀》城市賽鄭州站歡樂落幕城市賽戰報,《王者榮耀》城市賽鄭州站歡樂落幕城市賽戰報,《王者榮耀》城市賽鄭州站歡樂落幕</text>
</template>
<style scoped>
.info-text{
lines:1;
text-overflow:ellipsis;
}
</style>
<script>
export default {
data(){
return {
textStyle:{}
};
},
methods:{
textClick(){
this.textStyle = {
lines:0
}
}
}
}
</script>
</style>
<div class="wrapper">
<div class="box a">a</div>
<div class="box b">b</div>
<div class="box c">c</div>
<div class="d">d</div>
</div>
能夠看到web和ios、android的表現不一致,ios、android中是以代碼中dom順序來依次添加的,和z-index無關,後面加載的視圖會覆蓋前面的視圖。web
因此要保證web、ios、android三端表現一致,改變dom書寫順序便可。
<div class="d"></div>
<div class="box c"></div>
<div class="box b"></div>
<div class="box a"></div>
[cc lang="html"]
[/cc]
web、ios android
建議:fixed定位不會受父容器影響,若是須要超出限制,子元素能夠設置fixed
這種佈局方式在css中要作到很容易,而在weex中利用提供的flex佈局確很難實現,最後的解決方案是經過js動態設置文字與標籤父級的寬度,從而控制文字的溢出
<template>
<div class="wrapper" @appear="onappear">
<div class="info-container" ref='info-container'>
<div class="info" ref='info' :class="isFullText?['info-full']:[]">
<text class="nick-text">{{isFullText}}企鵝電競企鵝電競企鵝電競企鵝電競</text>
<img src="http://119.29.8.64/vipstyle/egame/app/weex/tab/ERICKCHEN-MC0/dist/static/img/live.b467410.png" class="live-image">
</div>
</div>
</div>
</template>
<style scoped lang="stylus">
.info-full{
width:300px;
}
</style>
<script>
const dom = weex.requireModule('dom')
export default {
data(){
return {
isFullText:false
};
},
methods:{
onappear(){
dom.getComponentRect(this.$refs['info'],option1=>{
dom.getComponentRect(this.$refs['info-container'],option2=>{
if(option1.size.width>=option2.size.width){
this.isFullText=true;
}
})
});
}
}
}
</script>
css的float能夠作到圖文混排,而weex只提供了flex佈局,而且text組件之間也不能進行嵌套,沒法作到這種圖文混排效果,不過weex的text組件比較奇特,那就是text組件中的空格是照代碼原樣輸出的,如
<text> 戰國鬼才傳,這個名字想必不少人聽都沒有聽過吧,這個名字說實話真的不是很吸引人…</text>
因此解決的方案能夠利用填充空格給圖片預留位置,先計算一個空格的寬度,再計算這張圖片所須要的空格數量,最後空格鏈接字符串輸出。
ios、android效果以下(紅色色塊爲圖片區域):
結構
<template>
<div class="wrapper">
<scroller class="scroller">
<div @appear="handleAppear">
<text>空格寬度:{{spaceWidth}}-空格數量:{{spaceNum}}</text>
<text class="demo-text" ref="demo-text1"> test</text>
<text class="demo-text" ref="demo-text2">test</text>
</div>
<div class="rich">
<div class="rich-icon"></div>
<text class="rich-text" :style="textStyle">{{content}}</text>
</div>
</scroller>
</div>
</template>
<style scoped>
.demo-text{
position: absolute;
font-size: 32px;/*文字大小與須要加空格文字大小保持一致*/
opacity: 0;
}
.rich{
position: relative;
}
.rich-icon{
position: absolute;
left:0;
top:4px;
width: 120px;
height: 32px;
background-color: red;
}
.rich-text{
font-size: 32px;
}
</style>
<script>
const dom = weex.requireModule('dom');
export default {
data(){
return {
spaceWidth:0,//空格寬度
spaceNum:0,//所需空格數量
opacity:0,//初始透明度爲0,避免文案抖動
content:'王者榮耀遊戲中的鑽石用來作什麼最合算?王者榮耀鑽石用來幹什麼最好?在王者榮耀中鑽石並非惟一通用的貨幣,在遊戲中還有金幣和點券,小編我的以爲鑽石在遊戲中並無其餘兩種貨幣有優點。'
};
},
computed:{
textStyle(){
return {
opacity:this.opacity
}
}
},
methods:{
handleAppear(){
setTimeout(()=>{
this.setTextContent();
},30)
},
async setTextContent(){
const text1El = this.$refs['demo-text1'];
const text2El = this.$refs['demo-text2'];
let textSize1,textSize2;
await this.getSpaceSize(text1El,(data)=>{
textSize1 = data;
});
await this.getSpaceSize(text2El,(data)=>{
textSize2 = data;
});
this.spaceWidth=Math.abs(textSize1-textSize2)/10;
this.content=this.getSpaceNum();
this.opacity=1;
},
getSpaceSize(el,callback){
return new Promise(function (resolve) {
dom.getComponentRect(el, option => {
if(option.result){
resolve(callback(option.size.width));
}
});
})
},
getSpaceNum(){
this.spaceNum = Math.ceil(120 / this.spaceWidth);//120爲紅色區塊寬度
return new Array(this.spaceNum).join(' ')+ this.content;
}
}
}
</script>
很遺憾,目前這種特殊文本以及圖片置於段落末尾並無找到相應的解決方案,只能依賴終端添加相應的富文本功能。
錯誤示例:
<template>
<div class="wrapper">
<Loading></Loading>/*改用其它名稱*/
</div>
</template>
<script>
import Loading from './demo'
export default {
components:{
Loading
}
}
</script>
weex輪播圖指示器效果:
電競項目輪播圖指示器效果
weex slider提供了change事件,能夠獲取到當前播放的序號,從而作到自定義輪播指示器。 可是當中遇到一個詭異問題:若是「div.indicator-item」的內容爲空的話,H5中的指示器並不會隨着圖片切換而變化(樣式不生效),div中須要添加內容才行
<div class="indicator-item" v-for="(item,index) in data" ...>
{{index}}/*添加內容,解決H5中class切換樣式不生效問題*/
</div>
<template>
<div class="slider-container" :style="sliderStyle">
<slider class="slider" :interval="interval" @change="change" :auto-play="autoPlay">
<div class="slider-item" v-for="img in data" >
<image class="slider-image" :style="sliderStyle" resize="cover" :src="img.src"></image>
</div>
</slider>
<div class="slider-indicator">
<div class="indicator-item" v-for="(item,index) in data" :class="[current == index ? 'indicator-active' : '']">
{{index}}
</div>
</div>
</div>
</template>
css3: [cc lang="css"]
[/cc] 效果如圖:weex中提供了transition,能夠傳入相應的style,經過setInterval控制動畫循環播放,但setInterval比較耗性能,建議終端對weex sdk進行改造,加入相應的循環播放功能
<template>
<div class="wrapper">
<div class="demo" ref="demo"></div>
</div>
</template>
<style scoped>
.demo{
width: 200px;
height: 200px;
background-color: gold;
}
</style>
<script>
import * as animation from './animation.js'
export default {
mounted() {
setTimeout(()=>{
setInterval(() => {
animation.run(this.$refs.demo);
}, 2100);
},200)
}
}
</script>
const animation = weex.requireModule('animation');
export function transition(el, opts,dd) {
let duration = dd || 400
if (!el) {
return Promise.resolve();
}
return new Promise(function (resolve) {
animation.transition(el, {
duration: duration,
timingFunction: 'linear',
delay: 0,
...opts
}, resolve);
})
}
export async function run(el, obj) {
await transition(el, {
styles: {
backgroundColor: 'red',
}
},0.0001)
await transition(el, {
styles: {
backgroundColor: 'purple',
}
},1000)
await transition(el, {
styles: {
backgroundColor: 'lime',
}
},1000)
}
優化建議:
一、儘可能不要設置背景色
二、不要過分嵌套,結構儘可能扁平化