js内容左右滑动切换的选项卡代码实例

快乐打工仔 分类:实例代码

分享一段代码实例,它实现了简单的tab选项卡功能。

选项卡的内容部分切换效果是左右水平滑动,代码实例如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.pipipi.net/" />
<title>犀牛前端部落</title>
<style>
* {
  margin: 0;
  padding: 0;
}
ui,li {
  list-style: none;
}
#list {
  overflow: hidden;
  margin: 30px 50px 0 50px;
}
#list li {
  float: left;
  width: 160px;
  height: 36px;
  border: #ddd 1px solid;
  line-height: 36px;
  text-align: center;
}
#list li.cur_li {
  background-color: #999;
  color: #fff;
}
.showbox {
  width: 482px;
  height: 300px;
  overflow: hidden;
  border: #ddd 1px solid;
  margin-left: 50px;
  border-top: none;
  position: relative;
}
#box_ul {
  overflow: hidden;
  width: 1446px;
}
#box_ul li {
  width: 482px;
  height: 300px;
  float: left;
}
</style>
<script>
window.onload = function() {
  show();
}
function show() {
  var oList = document.getElementById("list");
  var aLi = oList.getElementsByTagName("li");
  var oUl = document.getElementById("box_ul");
  oUl.style.position = "absolute";
  oUl.style.top = "0";
  oUl.style.left = "0";
  aLi[0].onmouseover = function() {
    for(var i=0; i<aLi.length; i++){
      aLi[i].className = "";
    }
    this.className = "cur_li";
    oneMoveElement("box_ul", 0, 0, 10);
  }
  aLi[1].onmouseover = function() {
    for(var i=0; i<aLi.length; i++){
      aLi[i].className = "";
    }
    this.className = "cur_li";
    oneMoveElement("box_ul", -482, 0, 10);
  }
  aLi[2].onmouseover = function() {
    for(var i=0; i<aLi.length; i++){
      aLi[i].className = "";
    }
    this.className = "cur_li";
    oneMoveElement("box_ul", -964, 0, 10);
  }
}
 
function oneMoveElement(id, final_x, final_y, interval) {
  var elem = document.getElementById(id);
  var xPos = parseInt(elem.style.left);
  var yPos = parseInt(elem.style.top);
  var dist = 0;
  if (elem.movement) {
    clearTimeout(elem.movement)
  }
  if (xPos < final_x) {
    dist = Math.ceil((final_x - xPos) / 10)
    xPos += dist;
  }
  if (xPos > final_x) {
    dist = Math.ceil((xPos - final_x) / 10)
    xPos -= dist;
  }
  if (yPos < final_y) {
    dist = Math.ceil((final_y - yPos) / 10)
    yPos += dist;
  }
  if (yPos > final_y) {
    dist = Math.ceil((yPos - final_y) / 10)
    yPos -= dist;
  }
  elem.style.top = yPos + "px";
  elem.style.left = xPos + "px";
  var repert = "oneMoveElement('" + id + "'," + final_x + "," + final_y + "," + interval + ")"
  elem.movement = setTimeout(repert, interval)
}
</script>
</head>
<body>
  <ul id="list">
    <li class="cur_li">犀牛前端部落一</li>
    <li style="border-left: none;border-right: none;">犀牛前端部落二</li>
    <li>犀牛前端部落三</li>
  </ul>
  <div class="showbox">
    <ul id="box_ul">
      <li style="background-color: red;"></li>
      <li style="background-color: royalblue;"></li>
      <li style="background-color: burlywood;"></li>
    </ul>
  </div>
</body>
</html>

js内容左右滑动切换的选项卡代码实例,这样的场景在实际项目中还是用的比较多的,关于js内容左右滑动切换的选项卡代码实例就介绍到这了。

js内容左右滑动切换的选项卡代码实例属于前端实例代码,有关更多实例代码大家可以查看

js内容左右滑动切换的选项卡代码实例

回复

我来回复
  • 暂无回复内容