原文:一些有用的javascript实例分析(三)

 10 输入两个数字,比较大小
window.onload = function ()
{
var aInput = document.getElementsByTagName("input");
var aSpan = document.getElementsByTagName("span")[0];
for(var i=0;i<aInput.length-1;i++){
aInput[i].onkeyup=function(){
//用空白取代非数字
this.value=this.value.replace(/^\d/,"");
}
aInput[2].onclick=function(){
//若都不输入数字提醒(保证代码的健壮性)
(aInput[0]==""||aInput[1]=="")?alert("please input number"):aSpan.innerHTML=Math.max(aInput[0].value,aInput[1].value)
}
}
11 简易网页时钟
window.onload = function ()
{
var oClock = document.getElementById("clock");
var aSpan = oClock.getElementsByTagName("span");
setInterval(getTimes,1000);
function getTimes(){
//获取当期时间
var date=new Date();
//时分秒组成一个数组
var aDate=[date.getHours(),date.getMinutes(),date.getSeconds()];
for(var i in aDate){aSpan[i].innerHTML=formate(aDate[i])}
}
function formate(a){
//正则匹配的反向引用,a是数字以默认的10进制输出
return a.toString().replace(/^(\d)$/,"0$1") }
12 setTimeout应用
window.onload = function ()
{
var oNav = get.byId("nav");
//获取一级导航栏
var aLi = get.byTagName("li", oNav);
//获取二级导航栏
var aSubNav = get.byClass("subnav", oNav);
var oSubNav = oEm = timer = null;
var i = 0;
//循环一级导航栏
for(i=1;i<aLi.length;i++){
aLi[i].onmouseover=function{
//先隐藏所有的子导航栏
for(i=0;i<aSubNav.length;i++){aSubNav[i].style.display="none";}
//获取该项下的子导航栏
oSubNav=get.byClass("subnav",this)[0];
//获取该项下的指示箭头
oEm=get.byTagName("em",this)[0];
//显示该项下的子导航栏
oSubNav.style.display="block";
//offsetWidth是指对象自身的宽度,整型,单位像素(含边线,如滚动条的占用的宽,值会随着内容的输入而不断改变)
//offsetLeft:获取对象相对于版面或由 offsetParent 属性指定的父坐标的计算左侧位置
//判断显示区域,this.offsetLeft是相对oNav来说的
oNav.offestWidth-this.offsetLeft>oSubNav.offestWidth?oSubNav.style.left=this.offest+"px":oSubNav.style.right=0;
//style.left,元素必须设置position才有效
oEm.style.left=this.offsetLeft-oSubNav.offsetLeft+50+"px";
clearTimeout(timer);
//阻止事件冒泡,只在li上有用,在nav没用
oSubNav.onmouseout=function(event){
(event||window.event).cancelBubble=true;
//清楚鼠标离开的定时器,做到无缝滑动
clearTimeout(timer);
}
}
aLi[i].onmouseout=function(){
//离开后0.3s去除子导航栏
timer=setTimeout(function(){oSubNav.style.display="none"},300)
}
}
}
13 自动播放效果-幻灯片
window.onload = function ()
{
var oBox = document.getElementById("box");
var aUl = document.getElementsByTagName("ul");
//获取图片列表
var aImg = aUl[0].getElementsByTagName("li");
//获取按钮列表
var aNum = aUl[1].getElementsByTagName("li");
var timer = play = null;
var i = index = 0;
//切换按钮
for(i=0;i<aNum.length;i++){
aNum[i].index=i;
aNum[i].onmouseover=function(){
show(this.index);
}
}
//鼠标滑过关闭定时器
oBox.onmouseover=function(){
clearInterval(play);
}
//鼠标离开启动自动播放
oBox.onmouseout=function(){
autoPlay();
}
//自动播放
function autoPlay(){
play=setInterval(function(){
//图片索引每两秒增加一次
index++;
//当大于图片数目时,归零。达到无缝滚动
index>=aImg.length&&(index=0)
show(index);
},2000)
}
autoPlay();
//图片切换,淡入淡出
function show(a){
index=a;
var alpha=0;
for(var i=0;i<aNum.length;i++){
//按钮样式
aNum[i].className="";}
aNum[index].className="current";
clearInterval(timer);
for(var i=0;i<aImg.length;i++){
//其他图片的透明度最高,即不显示
aImg[i].style.opacity = 0;//w3c
aImg[i].style.filter = "alpha(opacity=0)";//ie
}
timer=setInterval(function(){
alpha+=2//0.02秒加2
//若大于100,取100
alpha>100&&(alpha=100);
aImg[index].style.opacity = alpha / 100;
aImg[index].style.filter = "alpha(opacity = " + alpha + ")";
//当达到100,清楚定时器
alpha == 100 && clearInterval(timer)
},20) }
}
 14 拖拽
var zIndex = 1;
window.onload = function ()
{
var oDrag1 = document.getElementById("drag1");
var oDrag2 = document.getElementById("drag2");
drag(oDrag1);
drag(oDrag2);
};
function drag(oDrag){
var disX=disY=0;
oDrag.onmusedown=function(event){
var event=event||window.event;
disX=event.clientX-this.offsetLeft;
disY=event.clientY-this.offestTop;
var oTemp=document.createElement("div");
oTemp["id"]="temp";
oTemp.style.left=this.currentStyle?this.currentStyle["left"]:getComputeStyle(this,null)["left"];
oTemp.style.top = this.currentStyle ? this.currentStyle["top"] : getComputedStyle(this, null)["top"];
oTemp.style.zIndex=zIndex++;
document.body.appendChild(oTemp);
document.onmouseover=function(event){
var event = event || window.event;
var il=event.clientY-disX;
var iT=event.clientY-disY;
var maxL = document.documentElement.clientWidth - oDrag.offsetWidth;
var maxT = document.documentElement.clientHeight - oDrag.offsetHeight
iL <= 0 && (iL = 0);
iT <= 0 && (iT = 0);
iL >= maxL && (iL = maxL);
iT >= maxT && (iT = maxT);
oTemp.style.left=iL+"px";
oTemp.style.top=iT+"px";
return false;
};
document.onmouseup = function ()
{
document.onmousemove = null;
document.onmouseup = null;
oDrag.style.left = oTemp.style.left;
oDrag.style.top = oTemp.style.top;
oDrag.style.zIndex = oTemp.style.zIndex;
document.body.removeChild(oTemp);
oDrag.releaseCapture && oDrag.releaseCapture()
}; this.setCapture && this.setCapture();
return false
}
}

一些有用的javascript实例分析(三)的更多相关文章

  1. 一些有用的javascript实例分析(二)

    原文:一些有用的javascript实例分析(二) 5 求出数组中所有数字的和 window.onload = function () { var oBtn = document.getElement ...

  2. 一些有用的javascript实例分析(一)

    原文:一些有用的javascript实例分析(一) 本文以http://fgm.cc/learn/链接的实例索引为基础,可参见其实际效果.分析和整理了一些有用的javascript实例,相信对一些初学 ...

  3. C#保留2位小数几种场景总结 游标遍历所有数据库循环执行修改数据库的sql命令 原生js轮盘抽奖实例分析(幸运大转盘抽奖) javascript中的typeof和类型判断

    C#保留2位小数几种场景总结   场景1: C#保留2位小数,.ToString("f2")确实可以,但是如果这个数字本来就小数点后面三位比如1.253,那么转化之后就会变成1.2 ...

  4. Linux Kernel PANIC(三)--Soft Panic/Oops调试及实例分析【转】

    转自:https://blog.csdn.net/gatieme/article/details/73715860 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原 ...

  5. Linux I2C设备驱动编写(三)-实例分析AM3359

    TI-AM3359 I2C适配器实例分析 I2C Spec简述 特性: 兼容飞利浦I2C 2.1版本规格 支持标准模式(100K bits/s)和快速模式(400K bits/s) 多路接收.发送模式 ...

  6. 【转】Linux I2C设备驱动编写(三)-实例分析AM3359

    原文网址:http://www.cnblogs.com/biglucky/p/4059586.html TI-AM3359 I2C适配器实例分析 I2C Spec简述 特性: 兼容飞利浦I2C 2.1 ...

  7. [转载]JavaScript内存分析

    https://github.com/CN-Chrome-DevTools/CN-Chrome-DevTools/blob/master/md/Performance-Profiling/javasc ...

  8. javaScript高程第三版读书笔记

    看完<dom编程艺术>现在准备读进阶版的js高程了,由于篇幅较长,所以利用刚看完<dom编程艺术>学到的知识写了段JavaScript代码,来折叠各章的内容.并且应用到了< ...

  9. JavaScript中的三种弹出对话框

    学习过js的小伙伴会发现,我们在一些实例中用到了alert()方法.prompt()方法.prompt()方法,他们都是在屏幕上弹出一个对话框,并且在上面显示括号内的内容,使用这种方法使得页面的交互性 ...

随机推荐

  1. 乐在其中设计模式(C#) - 外观模式(Facade Pattern)

    原文:乐在其中设计模式(C#) - 外观模式(Facade Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 外观模式(Facade Pattern) 作者:webabcd 介绍 ...

  2. DirectShow基础编程 最简单transform filter 编写步骤

    目标编写一个transform filter,功能是对图像进行翻转. 一.选择基类 从CBaseFilter派生出三个用于编写transform filter的类,各自是:CTransformFilt ...

  3. UVALive 5103 Computer Virus on Planet Pandora Description 一些新兴需求模式的字符串 AC自己主动机

    主题链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=3104">点击打开链接 题意: ...

  4. cocos2dx3.1-lua移植android流程

    我很懒惰,写这篇博客只是为了能够转出后,当忘记查看,所以我写了下面非常简单的内容.假设完全没有经验的学生请找另一篇文章 一.环境配置(win7): 用户变量如下面: ANDROID_SDK_ROOT: ...

  5. javascript的位操作、整数、二进制

    位与(x&y):对操作数进行二进制与的操作,如果两个操作数的某一位两个都为1,将对应的结果位设为1. 0x0007 & 0x0003 = 0x0003 \ 一个小型年老棕色的狗:64 ...

  6. system strategies of Resources Deadlock

    In computer science, Deadlock is a naughty boy aroused by compete for resources. Even now,     there ...

  7. overflow的几个坑

    在android 4.0的原生浏览器上注意: html元素上不要加overflow: auto;的样式,否则会造成有些元素无法点击 在absolute元素上 不要加 overflow: auto; 否 ...

  8. Jeditable 点击编辑文字插件

    Jeditable - jQuery就地编辑插件使用   jeditable是一个jquery插件,它的优点是可以就地编辑,并且提交到服务器处理,是一个不可多得的就地编辑插件.(注: 就地编辑,也有称 ...

  9. Cocos2d-x 手机游戏《疯狂的蝌蚪》资源 “开源” win32+安德鲁斯+iOS三合一

    郝萌主倾心贡献,尊重作者的劳动成果,转载请注明出处 From郝萌主. 假设文章对您有所帮助.欢迎给作者捐赠,支持郝萌主,捐赠数额任意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载 ...

  10. 实例教程Unity3D单例模式(一)通经常使使用方法

    unity3d教程 中的单例模式通经常使使用方法 通经常使使用方法是在相关类增加GetInstance()的静态方法,检查实例是否存在.假设存在,则返回.假设不存在.则返回一个"须要用游戏元 ...