例题顺序:

1.子菜单下拉
2.图片轮播
3.选项卡效果
4.进度条制作
5.滑动效果
6.滚动固定效果

1.子菜单下拉

 <!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>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>子菜单下拉</title>
 <style>
     *{
         margin:0px auto;
         padding:0px;
         text-align:center;
         line-height:50px;
         vertical-align:middle;
         }
     #wai{
         margin-top:150px;
         width:800px;
         height:50px;
         }
     .nei{
         background-color:#9F9;
         float:left;
         width:150px;
         height:50px;
         line-height:50px;
         vertical-align:middle;
         }
     .neiw{
         width:0px;
         height:0px;
         float:left;
         display:none;}
     .nein{
         position:relative;
         top:50px;
         left:-150px;
         height:50px;
         width:150px;}
     .neil{
         margin:1px;
         width:149px;
         height:49px;
         background-color:#9F0;}
 </style>
 </head>

 <body>
     <div id="wai">
         <!--每个子菜单设置一个鼠标移上显示,一个鼠标移走隐藏-->
         <div class="nei" onmouseover="xianShi('cp')" onmouseout="yin('cp')">产品中心</div>
         <div class="neiw" id="cp">
             <div class="nein"  onmouseover="xianShi('cp')" onmouseout="yin('cp')">
                 <div class="neil">男装</div>
                 <div class="neil">女装</div>
                 <div class="neil">休闲</div>
             </div>
         </div>
         <div class="nei" onmouseover="xianShi('xw')" onmouseout="yin('xw')">新闻中心</div>
         <div class="neiw" id="xw"  onmouseover="xianShi('xw')" onmouseout="yin('xw')">
             <div class="nein">
                 <div class="neil">娱乐</div>
                 <div class="neil">军事</div>
                 <div class="neil">政策</div>
             </div>
         </div>

         <div class="nei" onmouseover="xianShi('jr')"  onmouseout="yin('jr')">今日动态</div>
         <div class="neiw" id="jr"  onmouseover="xianShi('jr')"  onmouseout="yin('jr')">
             <div  class="nein">
                 <div class="neil">男装</div>
                 <div class="neil">女装</div>
                 <div class="neil">休闲</div>
             </div>
         </div>
         <div class="nei" onmouseover="xianShi('zx')" onmouseout="yin('zx')">最新政策</div>
         <div class="neiw" id="zx">
             <div class="nein" onmouseover="xianShi('zx')" onmouseout="yin('zx')">
                 <div class="neil">商务</div>
                 <div class="neil">环境</div>
                 <div class="neil">金融</div>
             </div>
         </div>
     </div>
 </body>
 </html>
 <script>
     //鼠标移上去显示
     function xianShi(id){
         var d=document.getElementById(id);
         d.style.display="block";
     }
     //鼠标移走隐藏
     function yin(id){
         var d=document.getElementById(id);
         d.style.display="none";
     }

 </script>

这个题目需要注意的是不能只给子菜单设置外边距而不设置边框线,这样鼠标移到空白外边距时子菜单会触发隐藏效果

还有就是鼠标的事件加在子菜单的neiw和nein的div中,以及给每一个子菜单加鼠标事件,效果都是一样的

2.大图轮播效果

 <!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>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>大图轮播</title>
 <style>
     *{
         margin:0px auto;
         padding:0px;}
     /*设置外层div样式*/
     #wai{
         width:300px;
         height:600px;}
     /*设置图片大小,应和外层div一样,这里因为页面只有三个图片,所以直接用标签选择器设置*/
     img{
         width:300px;
         height:600px;}
 </style>
 </head>
 <body>
     <div id="wai">
             <img src="img/1 (3).jpg"/>
             <img src="img/1 (6).jpg"  style="display:none"/>
             <img src="img/1 (5).jpg"  style="display:none"/>
     </div>
 </body>
 </html>
 <script>
     //首先获取图片到数组中,这里因为页面只有三个图片没有其他内容,所以根据标签名获取
     var img=document.getElementsByTagName("img");
     //定义函数的索引值,也就是第几图片
     var index=0;
     //调用函数
     huan();
     //设置函数
     function huan(){
         //首先遍历所有图片设置隐藏
         for(i=0;i<img.length;i++){
             img[i].style.display="none";
         }
         //显示图片,如果这句话写在判断后面,会先显示第二张
         img[index].style.display="block";
         //索引加一,如果索引大于函数长度-1,索引在从0开始
         if(index>=img.length-1){
             index=0;
         }else{
             index++;
         }
         //这是轮播,每个两秒调用一次程序本身
         window.setTimeout("huan()",2000);
     }

 </script>

这里之前干过一个错误,把每个图片都放在一个div里,然后设置后两个图片所在的div默认隐藏,后面函数设置的的<img>标签显示,结果只有第一个图片显示两秒后消失。

<div>也不能乱加,看有无需要。

前后要对应好,不要前面设置的div隐藏,后面改变的img显示。

3.选项卡效果

 <!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>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>选项卡</title>
 <style>
     *{
         margin:0px auto;
         padding:0px;
         }
     #tou{
         margin-top:100px;
         width:800px;
         height:50px;
     }
     .list{
         float:left;
         width:200px;
         height:50px;
     }
     #shen{
         width:800px;
         height:50px;}
     .list1{
         float:left;
         width:800px;
         height:500px;
         display:none;
     }
 </style>
 </head>

 <body>
     <div id="tou">
         <div class="list" style="background-color:#0F9" onclick="show('s1')"></div>
         <div class="list" style="background-color:#9F0" onclick="show('s2')"></div>
         <div class="list" style="background-color:#9FF" onclick="show('s3')"></div>
         <div class="list" style="background-color:#3CF" onclick="show('s4')"></div>
     </div>
     <div id="shen">
           <div class="list1" style="background-color:#0F9" id="s1"></div>
         <div class="list1" style="background-color:#9F0" id="s2"></div>
         <div class="list1" style="background-color:#9FF" id="s3"></div>
         <div class="list1" style="background-color:#3CF" id="s4"></div>
     </div>
 </body>
 </html>
 <script>
     //单击选项卡,先将所有子菜单隐藏,在将被点击的选项卡的子菜单显示
     function show(id){
         var s=document.getElementsByClassName("list1");
         for(var i=0;i<s.length;i++){
             s[i].style.display="none";
         }
         var a=document.getElementById(id);
         a.style.display="block";
     }
 </script>

先布局选项卡,在布局子菜单,然后根据需要可在子菜单挨个设置隐藏,也可以在样式表统一设置隐藏,根据需要,这里在样式表设置

4.进图条制作

 <!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>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>进度条</title>
 <style>
     *{
         margin:0px auto;
         padding:0px;
         }
     #wai{
         margin-top:600px;
         width:800px;
         height:50px;
         border:1px solid #333;}
     #nei{
         float:left;
         background-color:#333;
         height:50px;
         }
 </style>
 </head>

 <body>
     <input type="button" value="开始" onclick="gun('nei')" />
     <div id="wai">
         <div id="nei" style="width:0%"></div>
     </div>
 </body>
 </html>
 <script>
     //设置循环,j代表进度,也就是宽度,一直加百分之一
     var j=0;
     function gun(id){
         if(j<100){
             j++;
         }
         //j是一个数值,用+和字符串%拼接起来之后变成了百分比字符串,字符串就可以被样式识别
         document.getElementById('nei').style.width=j+"%";
         window.setTimeout("gun()",5);
     }

     //以下是失败的方法
     /*function gun(id){
         var a=document.getElementById(id);
         var j=(parseInt(a.style.width));
         if(j<100){
             j++;
         }
         a.style.width=j+"%";
         window.setTimeout("gun()",5);
     }*/
 </script>

5.滑动效果

 <!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>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>无标题文档</title>
 <style>
     *{
         margin:0px auto;
         padding:0px;
         }
     #wai{
         width:1000px;
         height:500px;}
     #zuo{
         width:200px;
         height:500px;
         background-color:#3F0;
         float:left;}
     #you{
         width:800px;
         height:500px;
         background-color:#FF9;
         float:left;}
     #fu{
         width:40px;
         height:40px;
         background-color:#CCC;
         position:relative;
         text-align:center;
         line-height:40px;
         vertical-align:middle;
         top:-250px;
                 }
 </style>
 </head>

 <body>
     <div id="wai">
         <div id="zuo" style="width:200px"></div>
         <div id="you" style="width:800px"></div>
     </div>
     <div style="clear:both"></div>
     <div id="fu" style="left:-300px" onclick="dong()">>></div>
 </body>
 </html>
 <script>
     var z=document.getElementById("zuo");
     var y=document.getElementById("you");
     var s=document.getElementById("fu");
     function dong(){
         //获取每个div的宽度并转化为整数
         var i=parseInt(z.style.width);
         var k=parseInt(y.style.width);
         var sl=parseInt(s.style.left);
         //如果没有走到最终位置,就控制每个元素向右移动1
         if(i<800){
             i++;
             k--;
             sl++;
             z.style.width=i+"px";
             y.style.width=k+"px";
             s.style.left=sl+"px";
             window.setTimeout("dong()",6);

         }
     }
 </script>

6.滚动固定效果

 <!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>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>滚动固定</title>
 <style>
     *{
         margin:0px auto;
         padding:0px;
         }
     #wai{
         height:2200px;
         width:800px;
         }
     #tou{
         height:50px;
         width:800px;
         background-color:#0FF;
         }
     #shen{
         height:150px;
         width:800px;
         background-color:#9F6;}
 </style>
 </head>

 <body>
     <div id="wai">
         <div id="shen"></div>
         <div id="tou" ></div>
     </div>
 </body>
 </html>
 <script>
     //监听滚动的距离,滚动触发
     window.onscroll=function(){
         //获取头部菜单是否达到浏览器顶部边框
         if(window.scrollY>150){
             //设置头部菜单相对浏览器边框定位
             document.getElementById("tou").style.position="fixed";
             //距离顶部0距离
             document.getElementById("tou").style.top="0px";
             }else{
             //如果距离小于150,也就是滚回,设置定位为空,及不定位
             document.getElementById("tou").style.position="";
             }
     }
 </script>

最新文章

  1. Microservice架构模式简介
  2. 使用SwipeListView实现滑动效果
  3. 0,SFDC 开发篇 - 开发框架和APEX语法
  4. BizTalk动手实验(一)安装BizTalk Server 2010开发环境
  5. Intervals---poj1201(差分约束系统)
  6. poj1159 dp最长公共子串
  7. 数据备份--dump
  8. 【转】GitHub平台最火的iOS开源项目&mdash;&mdash;2013-08-25 17
  9. Difference Between Primes
  10. Luogu 考前模拟Round. 1
  11. Java Script 数组操作
  12. 安徽省2016“京胜杯”程序设计大赛_A_砝码称重
  13. “AOP代理”遇到“双上下文”
  14. 在 Ubuntu 14.04 中安装 Pepper Flash Player For Chromium
  15. 【AIM Tech Round 4 (Div. 2) D Prob】
  16. 【问题解决方案】从 Anaconda Prompt 或 Jupyter Notebook 终端进入Python后重新退出到命令状态
  17. 【BZOJ1417】Pku3156 Interconnect
  18. java项目中Excel文件的导入导出
  19. word,excel,ppt,txt转换为 PDF
  20. HDU3501——欧拉函数裸题

热门文章

  1. 最大流算法之EK(最短路径增广算法)
  2. IOS中的绘图Quartz2D
  3. Unity 消息发送机制 解析
  4. LeetCode5. Longest Palindromic Substring 最长回文子串 4种方法
  5. linux vi 报错 E37: No write since last change (add ! to override)
  6. 【转】纯手工玩转 Nginx 日志
  7. Day2-文件操作
  8. Hibernate学习笔记三:对象关系映射(一对一,一对多,多对一,多对多)
  9. Ehcache 整合Spring 使用页面、对象缓存(1)
  10. 关于IOS sourcetree 注册 2017最新hosts