本文介紹一個以 jQuery 外掛 splitScreen 實作的頁面分屏切換特效,可將內容區塊劃分為多個子畫面並自由切換版面配置,適合作為多視窗儀表板或監控牆的基礎元件。
頁面主要由 header、分屏渲染區與 footer 三個部分組成:
<div class="header"> header </div> <div class="container" id="displayArea"> <!-- 分屏內容渲染區域 --> </div> <div class="footer"> <!-- 省略其他代碼 --> </div>
先準備分屏資料陣列,再將容器與資料傳入 splitScreen 建構子即可初始化:
// 分屏數據
var iframes = [
{ id:'frames_1', frameName:'百度一下', src:'https://www.baidu.com' },
{ id:'frames_1', frameName:'百度地圖', src:'https://map.baidu.com' },
{ id:'frames_1', frameName:'百度視頻', src:'https://v.baidu.com' }
// 其餘資料略
];
// 自適應屏幕
window.onload = function(){ Panel.resize(); }
window.onresize = function(){ Panel.resize(); }
// 初始化分屏
var splitScreen = new splitScreen($('#displayArea'), iframes);
// 分屏切換
function changeModel(obj){
var fpmodel = $(obj).attr('data-fp');
if(fpmodel != "setting"){
splitScreen.screenMode(fpmodel, function(){
$(obj).addClass('currentLi').siblings().removeClass('currentLi');
});
}else{
splitScreen.toggleTopbar(function(){
$(obj).toggleClass('currentLi');
});
}
}
1. 定義類別,於初始化時呼叫 initialize:
var splitScreen = function(elem, options) {
this.elem = elem; // 分屏模塊渲染區域元素
this.options = options;// 分屏鏈接數據
this.initialize.apply(this);
}
2. 原型主要方法:
splitScreen.prototype = {
initialize: function(){}, // 初始化方法
screenMode: function(){}, // 分屏方法
renderPanel: function(){}, // 渲染分屏 DOM
bindPanel: function(){} // 事件監聽
};
此方法根據不同的模式切換對應的 CSS class,由樣式來控制分屏版面配置。這樣做的好處是可自由定義各塊的寬高大小,但相對較為繁瑣:
switch (Number(mode)) {
case 1:
this.data = [ ['fp-1-1'] ];
this.defaultShow = [0]; break;
case 2:
this.data = [ ['fp-2-1'], ['fp-2-2'] ];
this.defaultShow = [1, 2]; break;
case 3:
this.data = [ ['fp-3-1'], ['fp-3-2', 'fp-3-3'] ];
this.defaultShow = [1, 2, 3]; break;
case 4:
this.data = [ ['fp-4-1', 'fp-4-2'], ['fp-4-3', 'fp-4-4'] ];
this.defaultShow = [4, 1, 2, 3]; break;
default:
alert("不支持" + mode + "分屏");
}
每種分屏模式對應一組絕對定位的樣式,透過 top、left、right、bottom 精細控制各區塊的位置與大小:
.fp-box{position:absolute;border:1px solid #000;background:#fff;}
.fp-1-1{top:0;left:0;right: 0;bottom: 0;}
.fp-2-1{top:0;left:0;right: 300px;bottom: 0;}
.fp-2-2{top:0;right: 0px;bottom: 0; width: 300px;}
.fp-3-1{top:0;left:0;right: 300px;bottom: 0;}
.fp-3-2{top:0;right: 0;width:300px;height:50%;}
.fp-3-3{top:50%;right: 0;bottom: 0;width:300px;}
.fp-4-1{top:0;left:0;right: 50%;height:50%;}
.fp-4-2{top:50%;left:0;right: 50%;bottom: 0;}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>fp version2</title>
<style type="text/css">
*{margin:0;padding:0;}
.header{background:#ddd;height:120px;}
.footer { height: 40px; background: #ABABAB; position: fixed; bottom: 0px; width: 100%; }
.footer .ulTab {list-style-type: none;width:200px;overflow: hidden;float: left;}
.footer .ulTab li{float: left;height:16px;padding: 12px 15px;cursor: pointer;}
.footer .ulTab li.currentLi{background: #fff;}
.container{position: relative;}
.fp-box{position:absolute;border:1px solid #000;background:#fff;}
.fp-1-1{top:0;left:0;right: 0;bottom: 0;}
</style>
</head>
<body>
<div class="header"> header </div>
<div class="container" id="displayArea"></div>
<div class="footer">
<div class="footerCon clearFix">
<ul class="ulTab">
<li class="currentLi" data-fp ="1" onclick="changeModel(this)"></li>
<li data-fp ="3" onclick="changeModel(this)"></li>
<li data-fp ="6" onclick="changeModel(this)"></li>
<li data-fp ="setting" onclick="changeModel(this)" title="設置"></li>
</ul>
</div>
</div>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="js/splitScreen.js"></script>
<script type="text/javascript">
var iframes = [
{ id:'frames_1', frameName:'百度一下', src:'https://www.baidu.com' },
{ id:'frames_1', frameName:'百度地圖', src:'https://map.baidu.com' }
];
window.onload = function(){ Panel.resize(); }
window.onresize = function(){ Panel.resize(); }
var splitScreen = new splitScreen($('#displayArea'), iframes);
function changeModel(obj){
var fpmodel = $(obj).attr('data-fp');
splitScreen.screenMode(fpmodel, function(){
$(obj).addClass('currentLi').siblings().removeClass('currentLi');
});
}
</script>
</body>
</html>
外掛在 bindPanel 中透過事件代理綁定了 select 切換、放大、縮小與關閉等互動,並以 _on/fire 實作自訂事件的監聽與觸發:
var splitScreen = function(elem, options) {
this.elem = elem;
this.options = options;
this.initialize.apply(this);
}
splitScreen.prototype = {
initialize: function() {
this.handlers = {};
this.screenMode(1);
},
screenMode: function(mode, callback) {
this.elem.empty();
this.data = null;
this.defaultShow = null;
switch (Number(mode)) {
case 1:
this.data = [ ['fp-1-1'] ];
this.defaultShow = [0]; break;
default:
alert("不支持" + mode + "分屏");
}
if (this.data != null) {
this.renderPanel();
this.bindPanel();
}
callback && callback();
},
renderPanel: function(){
// 依 this.data 建立 fp-box 與 iframe,並插入 topbar 工具列
},
bindPanel: function(){
// 以事件代理處理 .fp-select 切換、.btnBig 放大、.btnSmall 縮小、.btnCls 關閉
},
toggleTopbar: function(callback){
if (this.elem.find('.topbarDiv:visible').length > 0) {
this.elem.find('.topbarDiv').hide();
} else {
this.elem.find('.topbarDiv').show();
}
callback && callback();
},
_on: function(type, handler) {
if (typeof this.handlers[type] == "undefined") {
this.handlers[type] = [];
}
this.handlers[type].push(handler);
return this;
},
fire: function(type, data) {
if (this.handlers[type] instanceof Array) {
var handlers = this.handlers[type];
for (var i = 0, len = handlers.length; i < len; i++) {
handlers[i](data);
}
}
}
};
// 版面自適應
var Panel = {
config: {
header: $('.header'),
container: $('.container'),
footer: $('.footer'),
win: $(window)
},
resize: function() {
var topH = Panel.config.header.height();
var botH = Panel.config.footer.height();
var mainH = Panel.config.win.height() - topH - botH;
mainH = mainH < 0 ? 100 : mainH;
Panel.config.container.height(mainH);
},
getSize: function() {
return { w: Panel.config.container.width(), h: Panel.config.container.height() };
}
};
以上便是以 splitScreen 外掛實作頁面分屏切換的完整說明,透過 CSS 控制版面、JS 負責渲染與互動的分工,即可靈活打造多視窗的分屏介面。