目录

写在前面

在完成了侧边栏后,后台的基础架子已经基本出来了;下面是来加入Tab页面,正在纠结于Tab页面是否用现成的轮子,

比如

LayUI

想一想还是撸起袖子直接干一个

Tabs

实现原理

Title 列表

头部标题部分,用于显示Tab页的标题和打开状态

Content 内容

用于放置Tab的内容,显示当前选中的内容

上代码

1. 布局

<!--tabs-->
<div class="ls-tab-container">
<!--头部-->
<div class="ls-tab-titles flex">
<div class="tab-title" data-id="1">
<span class="title">Tab1</span>
</div>
<div class="tab-title" data-id="2">
<span class="title">Tab2</span>
</div>
<div class="tab-title" data-id="3">
<span class="title">Tab3</span>
</div>
</div>
<!--内容-->
<div class="ls-tab-contents">
<div class="tab-content" data-for="1">
Tab1 Content
</div>
<div class="tab-content" data-for="2">
Tab2 Content
</div>
<div class="tab-content" data-for="3">
Tab3 Content
</div>
</div>
</div>
.ls-tab-container {
position: absolute;
top: 0px;
left: 5px;
right: 0px;
bottom: 5px;
} .ls-tab-container .ls-tab-titles {
height: 36px;
background: rgba(255, 255, 255, .8);
box-shadow: 0 0 6px rgba(196, 199, 202, .35);
} .ls-tab-container .ls-tab-contents {
position: absolute;
top: 40px;
left: 5px;
right: 5px;
bottom: 5px;
} .ls-tab-container .ls-tab-titles .tab-title {
height: 36px;
line-height: 36px;
text-align: center;
padding: 0 15px;
min-width: 30px;
max-width: 200px;
overflow: hidden;
} .ls-tab-container .ls-tab-titles .tab-title:hover {
background: rgba(238, 238, 238, .4);
cursor: pointer;
}

看效果

2. 选中状态

选中的头部和内的状态

···
<div class="tab-title active" data-id="1">
<span class="title">Tab1</span>
<i class="ls-icon ls-icon-close op-close"></i>
</div>
···
···
<div class="tab-content active" data-for="1">
Tab1 Content
</div>
···
.ls-tab-container .ls-tab-titles .tab-title.active {
background: rgba(238, 238, 238, .6);
background: rgba(238, 238, 238, .7);
border-bottom: none;
color: #1d7ce3;
} .ls-tab-container .ls-tab-contents .tab-content {
display: none;
} .ls-tab-container .ls-tab-contents .tab-content.active {
display: block!important;
}

看效果

3. 增加关闭按钮

···
<div class="tab-title active" data-id="1">
<span class="title">Tab1</span>
<i class="ls-icon ls-icon-close op-close"></i>
</div>
···
.ls-tab-container .ls-tab-titles .tab-title .ls-icon-close {
color: #999;
font-size: 12px;
padding: 1px;
border-radius: 2px;
margin-left: 5px;
} .ls-tab-container .ls-tab-titles .tab-title .ls-icon-close:hover {
background: #f39422;
color: #fff;
}

看效果

js 操作

···
/**
* Tab页初始化
* @param {选择器} selector
*/
tab: function(selector) {
var $tab = $(selector);
var $title_container = $tab.find(".ls-tab-titles");
var $tab_contents = $tab.find(".ls-tab-contents");
var $title = $tab.find(".ls-tab-titles .tab-title");
// 激活Tab
var activeTab = function($tabTitle) {
var tabId = $tabTitle.data("id");
if ($tabTitle.hasClass("active")) {
return;
}
$tab.find(".ls-tab-titles .tab-title.active").removeClass("active");
$tab.find(".ls-tab-contents .tab-content.active").removeClass("active");
$tabTitle.addClass("active");
$tab_contents.find(".tab-content[data-for='" + tabId + "']").addClass('active');
};
// 移除Tab
var removeTab = function($tabTitle) {
var tabId = $tabTitle.data("id");
if ($tabTitle.hasClass("active")) {
debugger
// 切换到临近的Tab
if ($tabTitle.next().length) {
activeTab($tabTitle.next());
} else if ($tabTitle.prev().length) {
activeTab($tabTitle.prev());
}
}
$tabTitle.remove();
$tab_contents.find(".tab-content[data-for='" + tabId + "']").remove();
};
//事件绑定 + //点击切换
$title.click(function() {
activeTab($(this));
});
//点击关闭
$title.find(".op-close").click(function() {
removeTab($(this).parent());
});
//tab 对象
var tab = {
/**
* 激活
* @param {tabId} selector
*/
active: function(id) {
activeTab($title_container.find(".tab-title[data-id='" + id + "']"));
},
/**
* 添加
* @param {tabObj} selector
*/
add: function(tabObj) {
// tabObj
var defaults = {
id: 0,
title: "tab",
content: "",
closable: true
};
tabObj = tabObj || {};
tabObj = $.extend(defaults, tabObj);
// 无内容,创建内容区域
if (!$tab_contents.length) {
$tab_contents = $("<div class='ls-tab-contents'></div>");
$tab.append($tab_contents);
}
$tab_contents.append("<div class='tab-content' data-for='" + tabObj.id + "'>" + tabObj.content + "</div>");
var $newTabTitle =
$("<div class='tab-title' data-id='" + tabObj.id + "'><span class='title'>" + tabObj.title + "</span></span></div>")
.click(function() {
activeTab($(this));
});
if (tabObj.closable) {
$newTabTitle.append(
$("<i class='ls-icon ls-icon-close op-close'></i>")
.click(function() {
removeTab($(this).parent());
}));
};
$title_container.append($newTabTitle);
activeTab($newTabTitle); },
/**
* 移除
* @param {tabId} selector
*/
remove: function(id) {
removeTab($title_container.find(".tab-title[data-id='" + id + "']"));
} };
return tab;
}
···

菜单增加点击事件

···
<li>
<a pid="2" class="node node-leaf" data-href="icon.html">
<i class="fa fa-list fa-fw" aria-hidden="true"></i>
<span>菜单管理</span>
</a>
</li>
···
(function($) {
$.extend(page, {
/**
* 初始化
*/
init: function() {
// 初始化 tab 栏
this.mainTab = this.tab('.ls-tab-container');
$(".node.node-leaf").click(function() {
var $this = $(this);
page.mainTab.add({
id: $this.attr("pid"),
title: $this.find("span").text(),
content: "<iframe scrolling='no' frameborder='0' style='height:100%;width:100%;position:absolute;' src='" + $this.data("href") + "'></iframe>"
})
});
}
});
page.init();
})(jQuery);

效果预览

欢迎批评指正

源码地址

https://github.com/LaosanShang/ls-admin-frontend

东拼西凑完成一个“前端框架”(4) - Tabs页的更多相关文章

  1. 东拼西凑完成一个“前端框架”(5) - Tabs操作

    目录 东拼西凑完成一个后台 "前端框架" (1) - 布局 东拼西凑完成一个后台 "前端框架" (2) - 字体图标 东拼西凑完成一个"前端框架&qu ...

  2. 最接近原生APP体验的高性能前端框架——MUI

      前  言 MUI有三大特点: 轻量 追求性能体验,是我们开始启动MUI项目的首要目标,轻量必然是重要特征: MUI不依赖任何第三方JS库,压缩后的JS和CSS文件仅有100+K和60+K 原生UI ...

  3. vue 前端框架

    什么是vue.js 1.vue是目前最火的一个前端框架,react 是最流行的前端框架(react除了开发网站,还可以开发手机APP,vue语法也是可以进行手机app开发的,需要借助于weex) 2. ...

  4. 推荐web前端框架bootstrap

    bootstrap是基于Jquery而开发的一个前端框架. 全中文的学习网站:http://www.runoob.com/bootstrap/bootstrap-tutorial.html 实际上就是 ...

  5. Vue.js教程 1.前端框架学习介绍

    Vue.js教程 1.前端框架学习介绍 什么是Vue.js 为什么要学习流行框架 什么是Vue.js Vue.js 是目前最火的一个前端框架,React是最流行的一个前端框架(React除了开发网站, ...

  6. 前端框架easyui layout, Tabs,tree

    一.三大前端框架的 1.easyui=jquery+html4(用来做后台的管理界面) 不要钱,开发速度快,不好看,不支持响应式 2.bootstrap=jquery+html5 好看,开发速度快,部 ...

  7. DHTMLX 前端框架 建立你的一个应用程序 教程(十一)--添加/删除表格中的记录

    添加/删除表格中的记录 我们的最终功能是在表格中添加删除 我们通过单机工具栏上的按钮来实现添加删除 当我们单击添加按钮的时候, 表单中 第一行默认填写New contact 光标自动聚焦 当用户点击删 ...

  8. Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架

    Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的. 历史 Bootstrap 是由 Twitter 的 ...

  9. Amaze UI 是一个移动优先的跨屏前端框架。 http://amazeui.org/

    http://amazeui.org/ Amaze UI 是一个移动优先的跨屏前端框架.... Amaze UI 以移动优先(Mobile first)为理念,从小屏逐步扩展到大屏,最终实现所有屏幕适 ...

随机推荐

  1. 力扣(LeetCode)颠倒二进制位 个人题解

    颠倒给定的 32 位无符号整数的二进制位. 示例 1: 输入: 00000010100101000001111010011100 输出: 0011100101111000001010010100000 ...

  2. 官宣!Amazon EMR正式支持Apache Hudi

    ​Apache Hudi是一个开源的数据管理框架,其通过提供记录级别的insert, update, upsert和delete能力来简化增量数据处理和数据管道开发.Upsert指的是将记录插入到现有 ...

  3. Linux考题(一)

    1.创建目录/data/oldboy,并且在该目录下创建oldboy.txt,然后在文件oldybos.txt里写入内容“inet addr:192.168.228.128  Bcast:192.16 ...

  4. Altium Designer 18 画keepout层与将keepout层转换成Mechanical1层的方法

    画keepout的方法 先选中Keepout层:然后 右键->Place->Keepout->然后选择要画圆还是线 Keepout层一般只用来辅助Layout,不能作为PCB的外形结 ...

  5. cesium定义线面

    面: var polygon = viewer.entities.add({ polygon : { hierarchy : { positions : null, holes : [{ positi ...

  6. css之文本两端对齐的两种解决方法

    说起文本对齐,大家都知道text-align,最常用的有left.right.center,今天我们说一下justify,也就是文本两端 对齐.说起来简单,但是有些小坑大家还是要注意的. 现在我们有这 ...

  7. scrapy请求传参-BOSS反爬

    scrapy请求传参-BOSS反爬 思路总结 首先boss加了反爬 是cookies的 爬取的内容为职位和职位描述 # -*- coding: utf-8 -*- import scrapy from ...

  8. 挑战10个最难的Java面试题(附答案)【上】

    欢迎添加华为云小助手微信(微信号:HWCloud002 或 HWCloud003),验证通过后,输入关键字"加群",加入华为云线上技术讨论群:输入关键字"最新活动&quo ...

  9. 一道随机函数题:由rand5()生成rand7()

    题目:已知rand5()函数能随机等概率的生成0, 1, 2, 3, 4,利用rand5()函数编写一个rand7()函数实现相似的功能. 分析:其实就是利用rand5()组合成一个更大范围的数,之后 ...

  10. 【Java库】如何使用优秀的加密库Jasypt来保护你的敏感信息?

    1 简介 今天我们介绍一个Java库-Jasypt,全称为Java Simplified Encryption,用于加密解密.它能够让开发者用花费最小的工作而把加密集成到项目中,并且不需要对加密/解密 ...