一些有用的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实例分析(三)的更多相关文章
- 一些有用的javascript实例分析(二)
原文:一些有用的javascript实例分析(二) 5 求出数组中所有数字的和 window.onload = function () { var oBtn = document.getElement ...
- 一些有用的javascript实例分析(一)
原文:一些有用的javascript实例分析(一) 本文以http://fgm.cc/learn/链接的实例索引为基础,可参见其实际效果.分析和整理了一些有用的javascript实例,相信对一些初学 ...
- C#保留2位小数几种场景总结 游标遍历所有数据库循环执行修改数据库的sql命令 原生js轮盘抽奖实例分析(幸运大转盘抽奖) javascript中的typeof和类型判断
C#保留2位小数几种场景总结 场景1: C#保留2位小数,.ToString("f2")确实可以,但是如果这个数字本来就小数点后面三位比如1.253,那么转化之后就会变成1.2 ...
- Linux Kernel PANIC(三)--Soft Panic/Oops调试及实例分析【转】
转自:https://blog.csdn.net/gatieme/article/details/73715860 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原 ...
- Linux I2C设备驱动编写(三)-实例分析AM3359
TI-AM3359 I2C适配器实例分析 I2C Spec简述 特性: 兼容飞利浦I2C 2.1版本规格 支持标准模式(100K bits/s)和快速模式(400K bits/s) 多路接收.发送模式 ...
- 【转】Linux I2C设备驱动编写(三)-实例分析AM3359
原文网址:http://www.cnblogs.com/biglucky/p/4059586.html TI-AM3359 I2C适配器实例分析 I2C Spec简述 特性: 兼容飞利浦I2C 2.1 ...
- [转载]JavaScript内存分析
https://github.com/CN-Chrome-DevTools/CN-Chrome-DevTools/blob/master/md/Performance-Profiling/javasc ...
- javaScript高程第三版读书笔记
看完<dom编程艺术>现在准备读进阶版的js高程了,由于篇幅较长,所以利用刚看完<dom编程艺术>学到的知识写了段JavaScript代码,来折叠各章的内容.并且应用到了< ...
- JavaScript中的三种弹出对话框
学习过js的小伙伴会发现,我们在一些实例中用到了alert()方法.prompt()方法.prompt()方法,他们都是在屏幕上弹出一个对话框,并且在上面显示括号内的内容,使用这种方法使得页面的交互性 ...
随机推荐
- ORACLE触发特定的解释
ORACLE PL/SQL编程八: 把触发器说透 本篇主要内容例如以下: 8.1 触发器类型 8.1.1 DML触发器 8.1.2 替代触发器 8.1.3 系统触发器 8.2 创建触发器 8.2.1 ...
- JAVA学习JSTL与EL
一.基础 1.EL(Expression Language):为了使jsp写起来更加简单,提供了在Jsp中简化表达式的方法 2.JSTL:(JSP Standard Tag Library)jstl标 ...
- 设计模式C++达到 1.辛格尔顿
实现类的单个案件的Singleton模式.该系统有一个类只有一个实例,而本实施例是容易的外部访问.所以容易控制的实例的数量,并且节省系统资源. 单的情况下通常与一些非本地静态对象的使用,对于这些对象, ...
- QrcodeWithLogo
package com.qrcode; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import ...
- Deploy Oracle 10.2.0.5 on Red Hat Enterprise Linux 6.4
一.Linux系统安装和配置 1.安装系统时选Desktop 2.设置eth0网卡为静态IP,加入子网掩码,网关,DNS.并配置自己主动启动 3.改动/etc/hosts.加入主机名和相应IP 4.禁 ...
- ZOJ1463:Brackets Sequence(间隙DP)
Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular seque ...
- 数据库开发——参照完整性——在外键中使用Delete on cascade选项
原文:数据库开发--参照完整性--在外键中使用Delete on cascade选项 原文: http://www.mssqltips.com/sqlservertip/2743/using-dele ...
- 使用更清晰DebugLog开发和调试工具
在开发和应用的开发和调试过程中难免会发现故障的过程中.我相信很多做iOS开发程序员Xcode的debug调试功能大加关注. 但在这样做Android开发过程中,却不那么方便,虽然IDE也提供了debu ...
- php脚本生成google play url的下载链接,下载apk并自动反编译后获取android版本号
需求: get the offer tracking link follow the redirect to get google play url Go to http: ...
- struts.xml在Action配置具体解释
在博客上我已经基本上解释struts.xml基本配置.配置过程最为基本的是action的动态配置. 一.Action的创建方法 1)实现Action接口 2)继承ActionSupport类,覆写当中 ...