如何在一个网站或者一个页面,去书写你的JS代码
// JavaScript Document
//如何在一个网站或者一个页面,去书写你的JS代码:
//1.js的分层(功能) : jquery(tools) 组件(ui) 应用(app), mvc(backboneJs)
//2.js的规划(管理) : 避免全局变量和方法(命名空间,闭包,面向对象) , 模块化(seaJs,requireJs)
window.onload = function(){
mv.app.toTip();
mv.app.toBanner();
mv.app.toSel();
mv.app.toRun();
};
var mv = {}; //命名空间
mv.tools = {};
mv.tools.getByClass = function(oParent,sClass){
var aEle = oParent.getElementsByTagName('*');
var arr = [];
for(var i=0;i<aEle.length;i++){
if(aEle[i].className == sClass){
arr.push(aEle[i]);
}
}
return arr;
};
mv.tools.getStyle = function(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}
else{
return getComputedStyle(obj,false)[attr];
}
};
mv.ui = {};
mv.ui.textChange = function(obj,str){
obj.onfocus = function(){
if(this.value == str){
this.value = '';
}
};
obj.onblur = function(){
if(this.value == ''){
this.value = str;
}
};
};
mv.ui.fadeIn = function(obj){
var iCur = mv.tools.getStyle(obj,'opacity');
if(iCur==1){ return false; }
var value = 0;
clearInterval(obj.timer);
obj.timer = setInterval(function(){
var iSpeed = 5;
if(value == 100){
clearInterval(obj.timer);
}
else{
value += iSpeed;
obj.style.opacity = value/100;
obj.style.filter = 'alpha(opacity='+value+')';
}
},30);
};
mv.ui.fadeOut = function(obj){
var iCur = mv.tools.getStyle(obj,'opacity');
if(iCur==0){ return false; }
var value = 100;
clearInterval(obj.timer);
obj.timer = setInterval(function(){
var iSpeed = -5;
if(value == 0){
clearInterval(obj.timer);
}
else{
value += iSpeed;
obj.style.opacity = value/100;
obj.style.filter = 'alpha(opacity='+value+')';
}
},30);
};
mv.ui.moveLeft = function(obj,old,now){
clearInterval(obj.timer);
obj.timer = setInterval(function(){
var iSpeed = (now - old)/10;
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
if(now == old){
clearInterval(obj.timer);
}
else{
old += iSpeed;
obj.style.left = old + 'px';
}
},30);
};
mv.app = {};
mv.app.toTip = function(){
var oText1 = document.getElementById('text1');
var oText2 = document.getElementById('text2');
mv.ui.textChange(oText1,'Search website');
mv.ui.textChange(oText2,'Search website');
};
mv.app.toBanner = function(){
var oDd = document.getElementById('ad');
var aLi = oDd.getElementsByTagName('li');
var oPrevBg = mv.tools.getByClass(oDd,'prev_bg')[0];
var oNextBg = mv.tools.getByClass(oDd,'next_bg')[0];
var oPrev = mv.tools.getByClass(oDd,'prev')[0];
var oNext = mv.tools.getByClass(oDd,'next')[0];
var iNow = 0;
var timer = setInterval(auto,3000);
function auto(){
if(iNow == aLi.length-1){
iNow = 0;
}
else{
iNow++;
}
for(var i=0;i<aLi.length;i++){
mv.ui.fadeOut(aLi[i]);
}
mv.ui.fadeIn(aLi[iNow]);
}
function autoPrev(){
if(iNow == 0){
iNow = aLi.length-1;
}
else{
iNow--;
}
for(var i=0;i<aLi.length;i++){
mv.ui.fadeOut(aLi[i]);
}
mv.ui.fadeIn(aLi[iNow]);
}
oPrevBg.onmouseover = oPrev.onmouseover = function(){
oPrev.style.display = 'block';
clearInterval(timer);
};
oNextBg.onmouseover = oNext.onmouseover = function(){
oNext.style.display = 'block';
clearInterval(timer);
};
oPrevBg.onmouseout = oPrev.onmouseout = function(){
oPrev.style.display = 'none';
timer = setInterval(auto,3000);
};
oNextBg.onmouseout = oNext.onmouseout = function(){
oNext.style.display = 'none';
timer = setInterval(auto,3000);
};
oPrev.onclick = function(){
autoPrev();
};
oNext.onclick = function(){
auto();
};
};
mv.app.toSel = function(){
var oSel = document.getElementById('sel1');
var aDd = oSel.getElementsByTagName('dd');
var aUl = oSel.getElementsByTagName('ul');
var aH2 = oSel.getElementsByTagName('h2');
for(var i=0;i<aDd.length;i++){
aDd[i].index = i;
aDd[i].onclick = function(ev){
var ev = ev || window.event;
var This = this;
for(var i=0;i<aUl.length;i++){
aUl[i].style.display = 'none';
}
aUl[this.index].style.display = 'block';
document.onclick = function(){
aUl[This.index].style.display = 'none';
};
ev.cancelBubble = true;
};
}
for(var i=0;i<aUl.length;i++){
aUl[i].index = i;
(function(ul){
var aLi = ul.getElementsByTagName('li');
for(var i=0;i<aLi.length;i++){
aLi[i].onmouseover = function(){
this.className = 'active';
};
aLi[i].onmouseout = function(){
this.className = '';
};
aLi[i].onclick = function(ev){
var ev = ev || window.event;
aH2[this.parentNode.index].innerHTML = this.innerHTML;
ev.cancelBubble = true;
this.parentNode.style.display = 'none';
};
}
})(aUl[i]);
}
};
mv.app.toRun = function(){
var oRun = document.getElementById('run1');
var oUl = oRun.getElementsByTagName('ul')[0];
var aLi = oUl.getElementsByTagName('li');
var oPrev = mv.tools.getByClass(oRun,'prev')[0];
var oNext = mv.tools.getByClass(oRun,'next')[0];
var iNow = 0;
oUl.innerHTML += oUl.innerHTML;
oUl.style.width = aLi.length * aLi[0].offsetWidth + 'px';
oPrev.onclick = function(){
if(iNow == 0){
iNow = aLi.length/2;
oUl.style.left = -oUl.offsetWidth/2 + 'px';
}
mv.ui.moveLeft(oUl,-iNow*aLi[0].offsetWidth,-(iNow-1)*aLi[0].offsetWidth);
iNow--;
};
oNext.onclick = function(){
if(iNow == aLi.length/2){
iNow = 0;
oUl.style.left = 0;
}
mv.ui.moveLeft(oUl,-iNow*aLi[0].offsetWidth,-(iNow+1)*aLi[0].offsetWidth);
iNow++;
};
};
如何在一个网站或者一个页面,去书写你的JS代码的更多相关文章
- 如何在一个网站或者一个页面规划JS
规划主要分为两部分:1.JS的分层,2.Js的规划 1.JS的分层(功能) 1-1.底层的库 : jquery 1-2.组件(ui) : 比如拖拽等,模块之间没有必然的联系,可以重复利用 1-3. ...
- Js 日期选择,可以的一个页面中重复使用本JS日历,兼容IE及火狐等主流浏览器,而且界面简洁、美观,操作体验也不错。
<html> <head> <title>Js日期选择器并自动加入到输入框中</title> <meta http-equiv="con ...
- 基于jquery的从一个页面跳转到另一个页面的指定位置的实现代码
比如 想跳到 mao.aspx 的页面 的div id="s" 的位置 那么 只用<a href="mao.aspx#s"> 就可实现跳转到指定位置 ...
- 常见的页面效果,相关的js代码
1.焦点图 $(document).ready(function(){ var i=0; var autoChange= setInterval(function(){ if(i<$(" ...
- 页面怎么引用外部css+js代码
外部css样式:把css样式写到一个文件内,方便使用,减少冗余. 如果使用的是外部css样式,页面怎么引用: 使用 <link rel="stylesheet" type=& ...
- 检测一个页面所用的时间的js
window.onload = function () { var loadTime = window.performance.timing.domContentLoadedEventEnd-wind ...
- jquery实现页面控件拖动效果js代码
;(function($) { var DragPanelId = "divContext"; var _idiffx = 0; var _idiffy = 0; var _Div ...
- html页面控制字体大小的js代码
dom对象控制显示文章字体大小的js代码 <head> <script type="text/javascript"> function check(siz ...
- 记录一个在线压缩和还原压缩js代码的工具
packer – javascript 压缩工具 http://dean.edwards.name/packer/ Javascript Beautifier ---可以恢复某些压缩工具压缩的js代码 ...
随机推荐
- stuff about set multiset map multimap
A lot of interviewers like to ask the candidates the difference between set and multiset(map and mul ...
- 知道创宇爬虫题--代码持续更新中 - littlethunder的专栏 - 博客频道 - CSDN.NET
知道创宇爬虫题--代码持续更新中 - littlethunder的专栏 - 博客频道 - CSDN.NET undefined 公司介绍 - 数人科技 undefined
- mysql常用的一些命令,用于查看数据库、表、字段编码
1.查看数据库支持的所有字符集 show character set;或show char set; 2.查看当前状态 里面包括当然的字符集设置 status或者\s ...
- Ubuntu 14.04 配置 Java SE
首先下载Java SE,下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html: 下载后把压缩包拷贝到自定义的目 ...
- http协议和web本质
转载:http://www.cnblogs.com/dinglang/archive/2012/02/11/2346430.html http协议和web本质 当你在浏览器地址栏敲入“http://w ...
- java利用commons-email发送邮件并进行封装
本例中利用commons-email发送邮件并进行封装,支持html内容和附件:Commons Email是Apache的Commons子项目下的一个邮件客户端组件,它是基于JavaMail的,大大简 ...
- C++的静态分发(CRTP)和动态分发(虚函数多态)的比较
虚函数是C++实现多态的工具,在运行时根据虚表决定调用合适的函数.这被称作动态分发.虚函数很好的实现了多态的要求,但是在运行时引入了一些开销,包括: 对每一个虚函数的调用都需要额外的指针寻址 虚函数通 ...
- Day 3 @ RSA Conference Asia Pacific & Japan 2016 (morning)
09.00 – 09.45 hrs Tracks Cloud, Mobile, & IoT Security A New Security Paradigm for IoT (Inter ...
- Linux内核源代码情景分析系列
http://blog.sina.com.cn/s/blog_6b94d5680101vfqv.html Linux内核源代码情景分析---第五章 文件系统 5.1 概述 构成一个操作系统最重要的就 ...
- iOS之ASIHttp简单的网络请求实现
描述: ASIHttpRequest是应用第三方库的方法,利用代码快,减少代码量,提高效率 准备工作: 一.导入第三方库ASIHttpRequest 二.会报很多的错,原因有两个,一个是要导入Xcod ...