淺析vue如何實現(xiàn)手機(jī)橫屏功能
功能背景
有些需求需要手動實現(xiàn)橫屏功能,比如點擊一個橫屏的圖標(biāo)將整個頁面90度翻轉(zhuǎn),再點擊退出橫屏回到正常頁面。

實現(xiàn)思路
一拿到這個需求很可能就被嚇到了,但簡單來說,就是點擊橫屏按鈕跳轉(zhuǎn)一個新頁面,通過 css樣式 的翻轉(zhuǎn)樣式來實現(xiàn),主要是計算橫屏的寬高比較麻煩,下面來看具體的代碼實現(xiàn)。
關(guān)鍵代碼
<view class="box">
<view class="jxcLandscape" :style="{width:newHeight+'px',height:newWidth+'px'}">
<view class="title_H">
<view @click="handleBack" class="image_H">
<img style="width:40rpx;height: 40rpx;margin-right: 8rpx;" src="@/static/screen.png" />
退出橫屏
</view>
</view>
<!--主要內(nèi)容區(qū)-->
</view>
</view>
css 樣式:
.box{
position: relative;
width: 100%;
height: 100vh;
overscroll-behavior: none;
}
.jxcLandscape{
padding: 10px;
transform: rotate(90deg); // 關(guān)鍵代碼
transform-origin: 0% 0%; // 關(guān)鍵代碼
position: absolute;
top: 0%;
left: 100%;
margin-left: 0;
}js 方法:
onLoad(){
let that=this
uni.getSystemInfo({
success: function (res) {
that.newWidth=res.windowWidth
that.tablenewWidth=res.windowWidth-50
if(res.platform=='android'){
this.getStatusBarHeight((height) => {
that.barHeight = height
that.newHeight=res.windowHeight-that.barHeight
})
}else{
// 這是蘋果操作系統(tǒng)
that.newHeight=res.windowHeight
}
}
})
},
methods:{
getStatusBarHeight(){
let barHeight = 51
if (uni.getSystemInfoSync().platform == "ios") {
// ios {}可傳參
this.callhandler('getStatusBarHeight', "", callBack);
}
if (uni.getSystemInfoSync().platform == "android") {
// Android
if (window.webkit) {
barHeight = window.webkit.getStatusBarHeight()
callBack(barHeight)
}
}
},
callhandler(name, data, callback) {
setupWebViewJavascriptBridge(function(bridge) {
bridge.callHandler(name, data, callback)
})
},
setupWebViewJavascriptBridge(callback) {
if (window.WebViewJavascriptBridge) {
return callback(window.WebViewJavascriptBridge)
}
if (window.WVJBCallbacks) {
return window.WVJBCallbacks.push(callback)
}
window.WVJBCallbacks = [callback]
let WVJBIframe = document.createElement('iframe')
WVJBIframe.style.display = 'none'
WVJBIframe.src = 'https://__bridge_loaded__'
document.documentElement.appendChild(WVJBIframe)
setTimeout(() => {
document.documentElement.removeChild(WVJBIframe)
}, 0)
}
}到此這篇關(guān)于淺析vue如何實現(xiàn)手機(jī)橫屏功能的文章就介紹到這了,更多相關(guān)vue手機(jī)橫屏內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue template當(dāng)中style背景設(shè)置不編譯問題
這篇文章主要介紹了vue template當(dāng)中style背景設(shè)置不編譯問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
讓webpack+vue-cil項目不再自動打開瀏覽器的方法
今天小編就為大家分享一篇讓webpack+vue-cil項目不再自動打開瀏覽器的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
VueJS設(shè)計與實現(xiàn)之淺響應(yīng)與深響應(yīng)詳解
這篇文章主要為大家介紹了VueJS設(shè)計與實現(xiàn)之淺響應(yīng)與深響應(yīng)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Vue導(dǎo)入excel文件的兩種方式(form表單和el-upload)
最近開發(fā)遇到一個點擊導(dǎo)入按鈕讓excel文件數(shù)據(jù)導(dǎo)入的需求,下面這篇文章主要給大家介紹了關(guān)于Vue導(dǎo)入excel文件的兩種方式,分別是form表單和el-upload兩種方法,需要的朋友可以參考下2022-11-11

