zoukankan      html  css  js  c++  java
  • document操作例题4-进度条与移动不变色

    六.进度条
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <style type="text/css">
    	*{
    		margin:auto
    	 }
    	#kuang{
    		height:30px;
    		border:2px solid #999}
    	#tiao{
    		height:30px;
    		background-color:red;
    		float:left}	
    </style>
    </head>
    
    <body>
    <div id="kuang" style="600px;">
    	<div id="tiao" style="0px;"></div>
    </div>
    <input type="button" value="前进" onclick="Qian()"/>
    <input type="button" value="后退" onclick="Hou()"/>
    </body>
    <script type="text/javascript">
    	var x;													//定义变量x
    	var y;
    	function Qian()
    	{	
    		 x=window.setInterval("Jindu()",1);					//将间隔操作赋值给变量x,同时执行每隔1毫秒执行一次函数			
    		 window.clearInterval(y);							//清除间隔变量y,在这可理解为当执行y操作时再执行x操作可以终止y操作
    	}
    	function Jindu()
    	{
    		var kuang=document.getElementById("kuang");				
    		var tiao=document.getElementById("tiao");
    		var kc=parseInt(kuang.style.width);					//将获取到的样式取整后赋值给变量kc
    		var tc=parseInt(tiao.style.width);
    		if(tc>=kc)											//条件判断
    		{
    			window.clearInterval(x);						//清除间隔变量x,此时x已存有间隔操作
    			return;											//条件符合的话用return终止函数向下执行。	
    		}
    		tc=tc+2;											//将宽度样式累加																																														
    		tiao.style.width=tc+"px";
    	}
    	function Hou()
    	{
    		y=window.setInterval("Houtui()",1);	
    		window.clearInterval(x);
    	}
    	function Houtui()
    	{
    		var kuang=document.getElementById("kuang");
    		var tiao=document.getElementById("tiao");
    		var kc=parseInt(kuang.style.width);
    		var tc=parseInt(tiao.style.width);
    		if(tc<=0)
    		{
    			window.clearInterval(y);
    			return;	
    		}
    		tc=tc-2;
    		tiao.style.width=tc+"px";	
    	}
    </script>
    </html>
    

     

     此题思路为用间隔操作对宽度进行累加,当条件判断长度满足时用return来终止函数执行,需要注意宽度样式必须写在内联里。

    七.单击选中后鼠标移动不变
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <style type="text/css">
    *{
    	margin:auto }
    #biao{
    	200px;
    	height:40px;
    	background-color:#999;
    	text-align:center;
    	line-height:40px;
    	vertical-align:middle;
    	border:1px solid #FFF}
    .ming{
    	200px;
    	height:40px;
    	background-color:blue;
    	text-align:center;
    	line-height:40px;
    	vertical-align:middle;
    	border:1px solid #FFF;
    	display:block}
    .ming,#biao:hover{
    	cursor:pointer}		
    </style>
    </head>
    
    <body>
    <div id="biao">姓名</div>
    <div class="ming" onclick="Xuan(this)" onmouseover="Shang(this)" onmouseout="Xia(this)">李四</div>
    <div class="ming" onclick="Xuan(this)" onmouseover="Shang(this)" onmouseout="Xia(this)">王五</div>
    <div class="ming" onclick="Xuan(this)" onmouseover="Shang(this)" onmouseout="Xia(this)">赵六</div>
    </body>
    <script type="text/javascript">
    function Shang(a)													//鼠标放上显示为红色
    {
    	a.style.backgroundColor="red";	
    }
    function Xia(a)														//鼠标移出时恢复原色蓝色,如果该元素属性值为1,让其显示为红色。
    {
    	a.style.backgroundColor="blue";
    	if(a.getAttribute("shu")==1)
    	{
    		a.style.backgroundColor="red";	
    	}	
    }
    function Xuan(a)													//因为只能选中一个,需要先清除所有属性样式,让其恢复默认。										
    {																	//然后让该选中元素显示为红色,添加一个自定义属性值为1。
    	var ming=document.getElementsByClassName("ming");
    	for(var i=0;i<ming.length;i++)
    	{
    		ming[i].removeAttribute("shu");
    		ming[i].style.backgroundColor="blue";	
    	}
    		a.setAttribute("shu","1");
    		a.style.backgroundColor="red";	
    }	
    </script>
    </html>
    

     

    此题思路在于需要设置一个自定义属性,用来保存选中状态,当进行其他操作时,再用条件判断,如果是选中状态则保留样式。

  • 相关阅读:
    UVA1349 Optimal Bus Route Design 最优巴士路线设计
    POJ3565 Ants 蚂蚁(NEERC 2008)
    UVA1663 Purifying Machine 净化器
    UVa11996 Jewel Magic 魔法珠宝
    NEERC2003 Jurassic Remains 侏罗纪
    UVA11895 Honorary Tickets
    gdb调试coredump(使用篇)
    使用 MegaCLI 检测磁盘状态并更换磁盘
    员工直接坦诚直来直去 真性情
    山东浪潮超越3B4000申泰RM5120-L
  • 原文地址:https://www.cnblogs.com/wyc1991/p/8750219.html
Copyright © 2011-2022 走看看