js模拟iostabs的功能写法

本文详细介绍了如何利用原生JS实现与iOS风格相匹配的标签导航功能,并且该实现兼容了不同版本的IE浏览器。通过定制化的CSS样式和逻辑代码,成功解决了不能使用jQuery等框架的问题。

这几天忙于北京移动这边CRM新版本统一框架高保真文档的开发,所以最近涉及js脚本的开发很少,昨天开始设计到统一框架的仿ios  tabs功能的开发,开发以前遇到以下问题:

1.鉴于项目的需要,不能使用jquery等框架。

2.要使用原生的js开发兼容ie个版本的ios tabs功能。

废话少说上代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>customerView</title>
<style>
.custom{width:98%; margin:3px auto auto;}
.custom .cusBox_title{width:100%; height:26px; background:url(NewTab_bg.png) repeat-x; border-left:1px solid #d7d7d7; border-right:1px solid #d7d7d7; padding-top:3px;}
.cusBox_newTab{height:24px; margin:0px auto auto;}
.NewTab_l{width:4px; height:24px; background:url(NewTab_l.gif) no-repeat left center;}
.NewTab_c{height:24px; background:url(NewTab_c.gif) repeat-x left center;}
.NewTab_r{width:4px; height:24px; background:url(NewTab_r.gif) no-repeat right center;}
.NewTab_wrap{margin-top:2px; width:100%; height:22px; position:relative; overflow:hidden;}
.NewTab_but_list{width:66px; position:absolute; height:19px; text-align:center; color:#748db0; font-size:12px; list-style:none; line-height:19px; cursor:pointer; z-index:1; top:0PX;}
.list01{left:1px;}
.list02{left:90px;}
.list03{left:180px;}
.list04{left:270px;}
.NewTab_but_hover{width:166px; height:29px; position:absolute; left:1px; top:0px;  /*text-align:center; line-height:19px; */background:url(NewTab_bg_1.gif) no-repeat; z-index:0;}
.custom .cusBox_con{width:100%;}
.NewTab_frame{border:1px solid #c6c6c6; border-top:none;}
</style>
</head>
<body>
<div class="custom">
<div class="cusBox_title">
<div class="cusBox_newTab" style="width:368px;">
<table border="0" cellpadding="0" cellspacing="0" width="100%" height="24px"><tr>
<td class="NewTab_l"></td>
<td class="NewTab_c" width="360px">
<div class="NewTab_wrap" id="NewIosTab1">
<div class="NewTab_but_list list01" value="http://www.baidu.com">营销列表</div>
<div class="NewTab_but_list list02" value="http://www.douban.com">服务请求</div>
<div class="NewTab_but_list list03" value="http://www.qq.com">集团客户</div>
<div class="NewTab_but_list list04" value="http://www.google.com.cn">渠道代理</div>
<div class="NewTab_but_hover" id="NewTab_listActive1"></div>
</div><!--end NewTab_wrap-->
	
</td>
<td class="NewTab_r"></td>
</tr></table>
</div><!--end cusBox_newTab-->
</div><!--end cusBox_title-->
<div class="cusBox_con" style="height:500px;">
<iframe src="http://www.baidu.com" frameborder="0" height="500px" width="100%" id="NewTab_frame1" class="NewTab_frame"></iframe>
</div><!--end cusBox_con-->
</div><!--end custom_box-->
<script language="javascript">
var NewIosTabs=function(){
	return{
		checkEffe:function(id1,id2,id3){
			var NewIosTabNodes = document.getElementById(id1).getElementsByTagName("div");//getElementsByName("name")只对表单元素起作用
			var NTL_Active = document.getElementById(id2);
			var NTL_frame = document.getElementById(id3);
			var NITNodesNum =NewIosTabNodes.length-1;
			for(var i=0; i<NITNodesNum; i++){
				NewIosTabNodes[i].onclick=function(){
					var NewIosNodesLeft = this.offsetLeft;
					var urlVal=this.getAttribute("value");
					NTL_Active.style.left=NewIosNodesLeft+"px";
					NTL_frame.setAttribute("src",urlVal);
				}
			}
		}
	}
}();
NewIosTabs.checkEffe("NewIosTab1","NewTab_listActive1","NewTab_frame1");
</script>
</body>
</html>


 

 

在使用 `el-tabs` 组件时,动态生成标签页是一种常见的需求,特别是在需要根据数据动态渲染不同标签内容的场景中。通过 `v-for` 指令循环生成 `el-tab-pane` 是实现这一功能的核心方式。以下是一个完整的实现示例。 ### 动态生成标签页 在 Vue 中,可以通过 `v-for` 循环一个数组来动态生成 `el-tab-pane` 组件。每个 `el-tab-pane` 的 `label` 和 `name` 属性可以绑定到数组中的对象字段。例如: ```html <template> <el-tabs v-model="activeName" @tab-click="handleClick"> <el-tab-pane v-for="item in tabsList" :key="item.name" :label="item.label" :name="item.name" > {{ item.content }} </el-tab-pane> </el-tabs> </template> <script> export default { data() { return { activeName: 'first', // 当前激活的标签页的 name tabsList: [ { label: '第一个标签', name: 'first', content: '第一个标签的内容' }, { label: '第二个标签', name: 'second', content: '第二个标签的内容' }, { label: '第三个标签', name: 'third', content: '第三个标签的内容' } ] }; }, methods: { handleClick(tab, event) { console.log(tab, event); } } }; </script> ``` 在上述代码中,`tabsList` 是一个包含标签页信息的数组,每个元素包含 `label`、`name` 和 `content`。通过 `v-for` 循环生成每个标签页,并将 `label` 和 `name` 绑定到 `el-tab-pane` 上。 ### 动态加载组件 如果每个标签页需要展示的是不同的组件,而不是简单的文本内容,则可以使用 `<component>` 元素配合 `:is` 来动态加载组件,并通过 `v-if` 控制是否渲染当前激活的组件[^2]。 ```html <template> <el-tabs v-model="activeName" type="card" @tab-click="handleClick"> <el-tab-pane v-for="item in tabsList" :key="item.name" :label="item.label" :name="item.name" > <component :is="item.component" v-if="activeName === item.name" /> </el-tab-pane> </el-tabs> </template> <script> import TabContentOne from './TabContentOne.vue'; import TabContentTwo from './TabContentTwo.vue'; import TabContentThree from './TabContentThree.vue'; export default { components: { TabContentOne, TabContentTwo, TabContentThree }, data() { return { activeName: 'first', tabsList: [ { label: '第一个标签', name: 'first', component: 'TabContentOne' }, { label: '第二个标签', name: 'second', component: 'TabContentTwo' }, { label: '第三个标签', name: 'third', component: 'TabContentThree' } ] }; }, methods: { handleClick(tab, event) { console.log(tab, event); } } }; </script> ``` 在上述代码中,`tabsList` 中的每个对象增加了一个 `component` 字段,用于指定当前标签页需要加载的组件。通过 `<component :is="item.component">` 动态加载组件,并通过 `v-if="activeName === item.name"` 控制是否渲染当前激活的组件。 ### 总结 通过 `v-for` 循环生成 `el-tab-pane`,可以实现 `el-tabs` 的动态标签页功能。如果需要动态加载不同的组件,则可以结合 `<component>` 和 `:is` 实现。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lixp3

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值