html控制自动切换选项卡速度,前端开发自动切换Tab选项卡

/p>

<

Tab选项卡(自动切换)

name=”viewport”

content=”width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no”

/>

* {

margin: 0;

padding: 0;

}

body {

overflow-x: hidden;

height: 100%;

}

body {

position: relative;

height: 100%;

background: #fff;

}

#tab {

width: 720px;

margin: 50px auto;

}

.tabNav {

overflow: hidden;

}

.tabNav span {

float: left;

height: 70px;

text-align: center;

}

.tabNav span em {

font-style: normal;

display: block;

height: 65px;

line-height: 70px;

font-size: 24px;

color: #999;

border-width: 4px 1px 1px;

border-style: solid;

border-color: #d8d8d8;

background-color: #f5f5f5;

margin-left: -1px;

}

.tabNav span.current em {

background-color: #e1e1e1;

border-top-color: #a1a1a1;

}

.tabContent .dc {

border-width: 0 1px 1px;

border-style: solid;

border-color: #d8d8d8;

display: none;

padding: 20px;

}

.tabContent div.current {

display: block;

}

标题1

标题2 标题3

标题4 标题5

标题6 标题7

标题8

内容1
内容2
内容3
内容4
内容5
内容6
内容7
内容8

var tab = function(obj, c) {

this.init(obj, c)

}

tab.prototype = {

init: function(obj, c) {

var self = this

if (self.getId(obj) == null) return

;(this.wrap = self.getId(obj)),

(this.con = self.getClass(obj, ‘dc’)),

(this.span = self

.getClass(obj, ‘tabNav’)[0]

.getElementsByTagName(‘span’)),

(this.com = c),

(this.myIndex = 0),

(this.m = ”),

(this.len = this.span.length)

this.render()

if (this.com.play == true) {

this.timer()

}

},

timer: function() {

;(this.time = null),

(self = this),

(t = this.com.speend || 3000)

clearInterval(self.time)

this.time = setInterval(function() {

self.myIndex++

if (self.myIndex == self.len) self.myIndex = 0

self.show(self.myIndex)

}, t)

},

render: function() {

var w = this.wrap.offsetWidth,

self = this

for (var i = 0; i < this.span.length; i++) {

this.span[i].style.width = parseInt(w / this.len) + ‘px’

this.span[i].index = i

this.span[i].onmouseover = function() {

clearInterval(self.time)

//console.log(this.index)

for (var k = 0; k < self.len; k++) {

self.span[k].className = ”

self.con[k].style.display = ‘none’

}

self.span[this.index].className = ‘current’

self.con[this.index].style.display = ‘block’

self.myIndex = this.index

}

this.span[i].onmouseout = function() {

if (self.com.play == true) {

self.timer()

}

}

}

},

show: function(n) {

for (var i = 0; i < this.len; i++) {

this.span[i].className = ”

this.con[i].style.display = ‘none’

}

this.span[n].className = ‘current’

this.con[n].style.display = ‘block’

},

getClass: function(obj, className) {

var a = this.getId(obj),

b = a.getElementsByTagName(‘*’),

c = []

for (var i = 0; i < b.length; i++) {

if (b[i].className == className) {

c.push(b[i])

}

}

return c

},

getId: function(obj) {

if (document.getElementById(obj) == null) return

return document.getElementById(obj)

},

}

new tab(‘tab’, { speend: 2500, play: true })

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在前端实现选项卡切换内容,可以使用HTML、CSS和JavaScript来实现。以下是一个简单的示例: HTML代码: ```html <div class="tab"> <button class="tablinks" onclick="openTab(event, 'tab1')">选项卡1</button> <button class="tablinks" onclick="openTab(event, 'tab2')">选项卡2</button> <button class="tablinks" onclick="openTab(event, 'tab3')">选项卡3</button> </div> <div id="tab1" class="tabcontent"> <h3>选项卡1</h3> <p>这是选项卡1的内容。</p> </div> <div id="tab2" class="tabcontent"> <h3>选项卡2</h3> <p>这是选项卡2的内容。</p> </div> <div id="tab3" class="tabcontent"> <h3>选项卡3</h3> <p>这是选项卡3的内容。</p> </div> ``` CSS代码: ```css .tab { overflow: hidden; border: 1px solid #ccc; background-color: #f1f1f1; } .tab button { background-color: inherit; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; } .tab button:hover { background-color: #ddd; } .tab button.active { background-color: #ccc; } .tabcontent { display: none; padding: 6px 12px; border: 1px solid #ccc; border-top: none; } ``` JavaScript代码: ```javascript function openTab(evt, tabName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } document.getElementById(tabName).style.display = "block"; evt.currentTarget.className += " active"; } ``` 这个示例中,我们使用了HTML的`button`元素来作为选项卡的标签,使用`div`元素来作为选项卡的内容。CSS用于定义选项卡的样式,JavaScript用于实现选项卡切换功能。 在JavaScript代码中,`openTab`函数接受两个参数,第一个参数是事件对象,第二个参数是要显示的选项卡的ID。函数首先隐藏所有选项卡的内容(通过将它们的`display`属性设置为`none`),然后将指定选项卡的内容显示出来(通过将其`display`属性设置为`block`),最后将当前选项卡的按钮标签的`active`类设置为激活状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值