完美实现js焦点轮播效果,两种方法

本文实例讲述了jQuery插件zTree实现获取当前选中节点在同级节点中序号的方法。分享给大家供大家参考,具体如下:

效果图:

最简单轮播形式,js中通过pic的display属性控制变换,也可通过调整Pic的margin-Left

1、实现代码:

图片 1

效果如图:

<!DOCTYPE html>
<html>
<head>
  <title>zTree实现基本树</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <link rel="stylesheet" type="text/css" href="zTree_v3/css/demo.css" rel="external nofollow" >
  <link rel="stylesheet" type="text/css" href="zTree_v3/css/zTreeStyle/zTreeStyle.css" rel="external nofollow" >
  <script type="text/javascript" src="zTree_v3/js/jquery-1.4.4.min.js"></script>
  <script type="text/javascript" src="zTree_v3/js/jquery.ztree.core.min.js"></script>
  <script type="text/javascript">
    <!--
    var setting = {
      data: {
        simpleData: {
          enable: true
        }
      }
    };
    var zNodes =[
      { id:1, pId:0, name:"湖北省", open:true},
      { id:11, pId:1, name:"武汉市", open:true},
      { id:111, pId:11, name:"汉口"},
      { id:112, pId:11, name:"汉阳"},
      { id:113, pId:11, name:"武昌"},
      { id:12, pId:1, name:"黄石市"},
      { id:121, pId:12, name:"黄石港区"},
      { id:122, pId:12, name:"西塞山区"},
      { id:123, pId:12, name:"下陆区"},
      { id:124, pId:12, name:"铁山区"},
      { id:13, pId:1, name:"黄冈市"},
      { id:131, pId:13, name:"黄州区"},
      { id:132, pId:13, name:"麻城市"},
      { id:133, pId:13, name:"武穴市"},
      { id:134, pId:13, name:"团风县"},
      { id:135, pId:13, name:"浠水县"},
      { id:136, pId:13, name:"罗田县"},
      { id:137, pId:13, name:"英山县"},
      { id:2, pId:0, name:"湖南省", open:true},
      { id:21, pId:2, name:"长沙市", open:true},
      { id:211, pId:21, name:"芙蓉区"},
      { id:212, pId:21, name:"天心区"},
      { id:213, pId:21, name:"岳麓区"},
      { id:214, pId:21, name:"开福区"},
      { id:22, pId:2, name:"株洲市"},
      { id:221, pId:22, name:"天元区"},
      { id:222, pId:22, name:"荷塘区"},
      { id:223, pId:22, name:"芦淞区"},
      { id:224, pId:22, name:"石峰区"}
    ];
    $(document).ready(function(){
      $.fn.zTree.init($("#baseTree"), setting, zNodes);
    });
    /**
     * 获取当前选中的第一个节点在同级节点中的序号
     */
    function indexNodes()
    {
      var treeObj = $.fn.zTree.getZTreeObj("baseTree");
      //获取选中的节点
      var nodes = treeObj.getSelectedNodes();
      if (nodes.length>0)
      {
        for(var i=0;i<nodes.length;i++)
        {
          var index = treeObj.getNodeIndex(nodes[i]);
          alert("获取当前选中的第一个节点在同级节点中的序号:"+index);
        }
      }
    }
    //-->
  </script>
 </head>
<body>
<div class="content_wrap">
  <div class="zTreeDemoBackground left">
    <ul id="baseTree" class="ztree" style="height: 300px; width:200px; overflow-y: auto"></ul>
    <input type="button" id="btn" onclick="indexNodes()" value="获取当前选中的第一个节点在同级节点中的序号"/>
  </div>
</div>
</body>
</html>

代码如下:

图片 2

2、实现效果图:

<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8">
 <script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
 <title>JQuery 源码分析</title>
 <style>
 #div1 div{width: 200px;height: 200px;border: 1px solid #FF0000;display: none;}
 .active{background: red;}
 *{margin: 0;padding: 0;}
 .tab:after{content: '';display: block;clear: both;}
 .tab li{width: 150px;height: 30px;line-height: 30px;text-align: center;cursor: pointer;list-style: none;float: left;margin: 0 10px;background: #ABCDEF;border-radius: 5px;}
 .tab li.active{background: #000;color:#fff;}
 .content:after{content: '';display: block;clear: both;}
 .content li{width: 460px;height: 300px;padding:20px;background: #f7f7f7;display: none;}
 </style>
 </head>
 <body>
 <div id="div1">
 <input class="active" type="button" value="1" />
 <input type="button" value="2"/>
 <input type="button" value="3"/>
 <div style="display: block;">11111111111</div>
 <div>22222222222</div>
 <div>333333333333</div>
 </div>
 <ul class="tab">
 <li class="active">1</li>
 <li>2</li>
 <li>3</li>
 </ul>
 <ul class="content">
 <li style="display: block;">111111111111</li>
 <li>222222222222</li>
 <li>333333333333</li>
 </ul>
 <script>
 $(function(){
 //jQuery 方法一
 $('#div1').find('input').click(function(){
 $('#div1').find('input').attr('class','');
 $('#div1').find('div').css('display','none')
 $(this).attr('class','active');
 $('#div1').find('div').eq($(this).index()).css('display','block');
 });
 //jQuery 方法二
 $('.tab').find('li').click(function(){
 var index = $(this).index();
 $(this).addClass('active').siblings().removeClass('active');
 $('.content').find('li').eq(index).show().siblings().hide();
 })
 })
 </script>
 </body>
</html>

实现代码:

(1)初始化

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

<!DOCTYPE html>
<html>
<head lang="en">
 <meta charset="UTF-8">
 <title></title>
 <style type="text/css">
*{
 margin: 0;
 padding: 0;
 list-style: none;
 text-decoration: none;
}
 .wrap{
 width: 490px;
 height: 170px;
 margin: 100px auto;
 border: 1px solid #000000;
 position: relative;
 overflow: hidden;
 }
 #pic{
 width: 2450px;
 height: 170px;
 }
 #pic li{
 float: left;
 }
 #list{
 position: absolute;
 bottom: 10px;
 left:150px ;
 }
 #list li{
 float: left;
 width: 15px;
 height: 15px;
 background: #fff;
 margin: 0 10px;
 border-radius: 50%;
 cursor: pointer;
 }
 #list .on{
 background: #e27a00;
 }
 .Prev{
 top: 30px;
 left: 0;
 }
 .Next{
 top: 30px;
 right: 0;
 }
 .Prev,.Next{
 position: absolute;
 font-size: 80px;
 font-weight: bold;
 color:#fff ;
 -webkit-transition: all 0.35s ease-in-out
 }
 .Next:hover,
 .Prev:hover{
 background: #ccc;
 background: rgba(204, 204, 204, 0.4);
 }
 .show{
 display: block;
 }
 .hidden{
 display: none;
 }
 </style>
 <script type="text/javascript">
 window.onload=function(){
 var pic=document.getElementById('pic').getElementsByTagName('li');
 var list=document.getElementById('list').getElementsByTagName('li');
 var prev=document.getElementById('Prev');
 var next=document.getElementById('Next');
 var index=0;
 var timer=null;

 auto();
 for(var i=0;i<list.length;i++){
 list[i].index=i;
 list[i].onmouseover=function(){
  clearInterval(timer);
  Change(this.index);
 }
 list[i].onmouseout=function(){
  auto();
 }
 pic[i].onmouseover=function(){
  clearInterval(timer);
 }
 pic[i].onmouseout=function(){
  auto();
 }
 }
 prev.onclick=function(){
 clearInterval(timer);
 index--;
 if(index<=0){
  index=list.length-1;
 }
 Change(index);
 }
 next.onclick=function(){
 clearInterval(timer);
 index++;
 if(index>=list.length){
  index=0;
 }
 Change(index);
 }
 prev.onmousemove=function(){
 clearInterval(timer);
 }
 prev.onmouseout=function(){
 auto();
 }
 next.onmouseover=function(){
 clearInterval(timer);
 }
 next.onmouseout=function(){
 auto();
 }

 function Change(curIndex){
  for(var i=0;i<list.length;i++){
  list[i].className="";
  pic[i].className="hidden";
  }
  list[curIndex].className="on";
  pic[curIndex].className="show";
 index=curIndex;
 }
 function auto(){
 timer=setInterval(function(){
  index++;
  if(index>=list.length){
  index=0
  }
  Change(index);
 },2000);
 }
 }
 </script>
</head>
<body>
<div class="wrap" id="wrap">
 <ul id="pic">
 <li class="show"><a href="#"><img src="/54111cd9000174cd04900170.jpg" alt="图片 3"/></a></li>
 <li><a href="#"><img src="/54111dac000118af04900170.jpg" alt="图片 4"/></a></li>
 <li><a href="#"><img src="/54111d9c0001998204900170.jpg" alt="图片 5"/></a></li>
 <li><a href="#"><img src="/54111d8a0001f41704900170.jpg" alt="图片 6"/></a></li>
 <li><a href="#"><img src="/54111d7d00018ba604900170.jpg" alt="图片 7"/></a></li>
 </ul>
 <ul id="list">
 <li class="on"></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 </ul>
 <a href="javascript:;" class="Prev" id="Prev"><</a>
 <a href="javascript:;" class="Next" id="Next">></a>
</div>
</body>
</html>

图片 8

您可能感兴趣的文章:

  • 基于jQuery实现选项卡效果
  • jquery Banner轮播选项卡
  • jQuery EasyUI Tab
    选项卡问题小结
  • 基于jquery实现最简单的选项卡切换效果
  • 基于JQuery的6个Tab选项卡插件
  • jquery tools之tabs
    选项卡/页签
  • jQuery之选项卡的简单实现
  • JQuery
    Tab选项卡效果代码改进版
  • jQuery实现选项卡切换效果简单演示

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

发表评论

电子邮件地址不会被公开。 必填项已用*标注