@fantaghiro
2015-02-03T10:43:49.000000Z
字数 43776
阅读 2894
学习笔记
js
前端
妙味课堂
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js分页</title>
<style>
a {margin: 5px;}
</style>
<script>
window.onload = function(){
page({
id : 'div1',
nowNum : 7,
allNum : 10,
callBack : function(now, all){
alert('当前页:' + now + ',总共页:' + all);
}
});
}
function page(opt){
if(!opt.id){return false};
var obj = document.getElementById(opt.id);
var nowNum = opt.nowNum || 1;
var allNum = opt.allNum || 5;
var callBack = opt.callBack || function(){};
if(nowNum >= 4 && allNum >=6){
var oA = document.createElement('a');
oA.href = '#1';
oA.innerHTML = '首页';
obj.appendChild(oA);
}
if(nowNum >=2){
var oA = document.createElement('a');
oA.href = '#' + (nowNum -1);
oA.innerHTML = '上一页';
obj.appendChild(oA);
}
if(allNum <= 5){
for(var i=1; i<=allNum; i++){
var oA = document.createElement('a');
oA.href = '#' + i;
if(nowNum == i){
oA.innerHTML = i;
} else {
oA.innerHTML = '[' + i + ']';
}
obj.appendChild(oA);
}
} else {
for(var i=1; i<=5; i++){
var oA = document.createElement('a');
if(nowNum == 1 || nowNum == 2){
oA.href = '#' + i;
if(nowNum == i){
oA.innerHTML = i;
} else {
oA.innerHTML = '[' + i + ']';
}
} else if((allNum - nowNum) == 0 || (allNum - nowNum) == 1 ){
oA.href = '#' + (allNum - 5 + i);
if((allNum - nowNum) == 0 && i == 5){
oA.innerHTML = (allNum - 5 + i);
} else if ((allNum - nowNum) == 1 && i == 4){
oA.innerHTML = (allNum - 5 + i);
} else {
oA.innerHTML = '[' + (allNum - 5 + i) + ']';
}
} else {
oA.href = '#' + (nowNum - 3 + i);
if(i == 3){
oA.innerHTML = nowNum - 3 + i;
} else {
oA.innerHTML = '[' + (nowNum - 3 + i) + ']';
}
}
obj.appendChild(oA);
}
}
if((allNum - nowNum) >= 1){
var oA = document.createElement('a');
oA.href = '#' + (nowNum + 1);
oA.innerHTML = '下一页';
obj.appendChild(oA);
}
if((allNum - nowNum) >= 3 && allNum >= 6){
var oA = document.createElement('a');
oA.href = '#' + allNum;
oA.innerHTML = '尾页';
obj.appendChild(oA);
}
callBack(nowNum, allNum);
var aA = obj.getElementsByTagName('a');
for(var i=0; i<aA.length; i++){
aA[i].onclick = function(){
var nowNum = parseInt(this.getAttribute('href').substring(1));
obj.innerHTML = '';
page({
id : opt.id,
nowNum : nowNum,
allNum : allNum,
callBack : callBack
})
return false; //阻止默认事件,避免#数字添到地址栏
}
}
}
</script>
</head>
<body>
<div id="div1">
<!-- <a href="#1">首页</a>
<a href="#3">上一页</a>
<a href="#2">[2]</a>
<a href="#3">[3]</a>
<a href="#4">4</a>
<a href="#5">[5]</a>
<a href="#6">[6]</a>
<a href="#5">下一页</a>
<a href="#10">尾页</a> -->
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>选中分享</title>
<style>
#p1 {width: 300px; margin: 50px;}
#div1 {position: absolute; display: none;}
</style>
<script>
window.onload = function(){
function selectText(){
if(document.selection){ //针对ie
return document.selection.createRange().text; //返回选中的文字
} else { //标准
return window.getSelection().toString();
}
}
var oP = document.getElementById('p1');
var oDiv = document.getElementById('div1');
oP.onmouseup = function(ev){
var ev = ev || window.event;
var left = ev.clientX;
var top = ev.clientY;
if(selectText().length > 10){
setTimeout(function(){
oDiv.style.display = 'block';
oDiv.style.left = left + 'px';
oDiv.style.top = top + 'px';
}, 100);
} else {
oDiv.style.display = 'none';
}
}
oP.onclick = function(ev){
var ev = ev || window.event;
ev.cancelBubble = true;
}
document.onclick = function(){
oDiv.style.display = 'none';
}
oDiv.onclick = function(){
window.location.href = 'http://v.t.sina.com.cn/share/share.php?searchPic=false&title=' + selectText() + '&url=http://www.baidu.com';
}
}
</script>
</head>
<body>
<p id="p1">
关于腾讯企业邮,你不知道的那些事:一、无需登录邮箱,可用微信直接收发邮件二、邮箱绑定微信,可及时获取新邮件通知三、微信动态密码,可确保邮箱安全不被盗超过40万家公司已经在使用腾讯企业邮,每天超过800家企业正在加入。为了节省您的时间,请您 在线登记,腾讯企业邮团队将为您提供详尽的咨询服务。要了解更多信息,您还可以登录腾讯企业邮官网
</p>
<div id="div1"><img src="img/share.gif" alt=""></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>无缝切换</title>
<style>
* {margin: 0; padding: 0;}
li {list-style: none;}
#div1 {width: 350px; height: 60px; border: 1px #000 solid; position: relative; margin: 0 auto; overflow: hidden;}
#div1 ul {position: absolute; left: 0;}
#div1 ul li {width: 80px; height: 60px; margin-right: 10px; float: left;}
img {width: 80px; height: 60px;}
</style>
<script src="move.js"></script>
<script>
window.onload = function(){
var oInput = document.getElementById('input1');
var oUl = document.getElementById('ul1');
var aLi = oUl.getElementsByTagName('li');
var oneSize = aLi[0].offsetWidth + 10; //10是margin
var iNum = 4;
var bBtn = true;
function getWidth(){
oUl.style.width = oneSize * aLi.length + 'px';
}
getWidth();
// 复制节点的方法: cloneNode(boolean) : 如果参数为true,就是一起复制里面的子节点;如果参数为false,就只复制当前节点,不复制里面的子节点。
oInput.onclick = function(){
if(bBtn){
bBtn = false;
for(var i=0; i<iNum; i++){
var oLi = aLi[i].cloneNode(true);
oUl.appendChild(oLi);
getWidth();
}
startMove(oUl, {
left : - iNum * oneSize
}, function(){
for(var i=0; i<iNum; i++){
oUl.removeChild(aLi[0]);
oUl.style.left = 0;
}
bBtn = true;
})
}
}
}
</script>
</head>
<body>
<input type="button" value="切换" id="input1">
<div id="div1">
<ul id="ul1">
<li><img src="photo/1.jpg"></li>
<li><img src="photo/2.jpg"></li>
<li><img src="photo/3.jpg"></li>
<li><img src="photo/4.jpg"></li>
<li><img src="photo/5.jpg"></li>
</ul>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>百叶窗效果</title>
<style>
* {margin: 0; padding: 0;}
li {list-style: none;}
#ul1, #ul2 {width: 300px; height: auto; float: left; border-top: 1px #000 solid; margin: 20px;}
li {width: 100%; height: 30px; overflow: hidden; position: relative; border-bottom: 1px dashed #333; line-height: 30px;}
li div {position: absolute; top: -30px;}
li div p {height: 30px;}
</style>
<script src="move.js"></script>
<script>
window.onload = function(){
var oUl = document.getElementById('ul1');
var oUl2 = document.getElementById('ul2');
toShow(oUl);
setTimeout(function(){
toShow(oUl2);
}, 2000);
function toShow(obj){
var iNow = 0;
var timer = null;
var target = -30;
var aDiv = obj.getElementsByTagName('div');
setInterval(function(){
toChange();
}, 4000);
function toChange(){
target = target == 0 ? -30 : 0;
timer = setInterval(function(){
if(iNow == aDiv.length){
clearInterval(timer);
iNow = 0;
} else {
startMove(aDiv[iNow], {top: target})
iNow++;
}
}, 100);
}
}
}
</script>
</head>
<body>
<ul id="ul1">
<li>
<div>
<p>11111111</p>
<p>22222222</p>
</div>
</li>
<li>
<div>
<p>33333333</p>
<p>44444444</p>
</div>
</li>
<li>
<div>
<p>55555555</p>
<p>66666666</p>
</div>
</li>
<li>
<div>
<p>7777777</p>
<p>8888888</p>
</div>
</li>
<li>
<div>
<p>9999999</p>
<p>8888888</p>
</div>
</li>
<li>
<div>
<p>7777777</p>
<p>6666666</p>
</div>
</li>
</ul>
<ul id="ul2">
<li>
<div>
<p>11111111</p>
<p>22222222</p>
</div>
</li>
<li>
<div>
<p>33333333</p>
<p>44444444</p>
</div>
</li>
<li>
<div>
<p>55555555</p>
<p>66666666</p>
</div>
</li>
<li>
<div>
<p>7777777</p>
<p>8888888</p>
</div>
</li>
<li>
<div>
<p>9999999</p>
<p>8888888</p>
</div>
</li>
<li>
<div>
<p>7777777</p>
<p>6666666</p>
</div>
</li>
</ul>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>微博发布框</title>
<style>
body {font-size: 12px;}
#div1 {width: 400px; margin: 20px auto;}
#div1 p {float: right; margin: 0;}
#div1 textarea {width: 400px; height: 100px;}
#div1 a {width: 50px; height: 30px; font-size: 16px; line-height: 30px; text-align: center; float: right; background: #00ff00; color: #fff;}
#div1 a.dis {background: #cccccc; color; #666}
</style>
<script>
//onchange: 当光标消失的时候,只能触发一次
// ie : onpropertychange : 输入连续触发
// 标准:oninput : 也是连续触发
window.onload = function(){
var oDiv = document.getElementById('div1');
var oP = oDiv.getElementsByTagName('p')[0];
var oT = oDiv.getElementsByTagName('textarea')[0];
var oA = oDiv.getElementsByTagName('a')[0];
var ie = !-[1,];
var bBtn = true;
var timer = null;
var iNum = 0;
oT.onfocus = function(){
if(bBtn){
oP.innerHTML = '打击虚假消息,建设文明微博,还可以输入<span>140</span>字';
bBtn = false;
}
}
oT.onblur = function(){
if(oT.value == ''){
oP.innerHTML = '《新浪微博社区公约(征求意见稿)》意见征求';
bBtn = true;
}
}
/*oT.onchange = function(){
console.log(1);
}*/
if(ie){
oT.onpropertychange = toChange;
} else {
oT.oninput = toChange;
}
function toChange(){
//英文字母算半个字;一个中文算一个字
var num = Math.ceil(getLength(oT.value)/2);
var oSpan = oDiv.getElementsByTagName('span')[0];
if(num <= 140){
oSpan.innerHTML = 140 - num;
oSpan.style.color = '';
} else {
oSpan.innerHTML = num - 140;
oSpan.style.color = 'red';
}
if(oT.value == '' || num > 140){
oA.className = 'dis';
} else {
oA.className = '';
}
}
oA.onclick = function(){
if(this.className == 'dis'){
clearInterval(timer);
timer = setInterval(function(){
if(iNum == 5){
clearInterval(timer);
iNum = 0;
} else {
iNum++;
}
if(iNum%2){
oT.style.background = 'red';
} else {
oT.style.background = '';
}
}, 100);
} else {
alert('发布成功!');
}
}
function getLength(str){
return String(str).replace(/[^\x00-\xff]/g, 'aa').length;
//用正则去匹配中文字符,找到一个中文字符,就用两个字母(比如aa)来替换,这样一个中文汉字就对应了两个字母的长度
//String(str) 不管传进来的参数str是什么,都转成字符串格式
}
}
</script>
</head>
<body>
<div id="div1">
<p>《新浪微博社区公约(征求意见稿)》意见征求</p>
<textarea></textarea>
<a class="dis" href="javascript:">发布</a>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>微博登陆</title>
<style>
* {margin: 0; padding: 0;}
body {font: 12px/1.125 Arial, Helvetica, sans-serif;}
li {list-style: none;}
#login {width: 252px; height: 385px; background: #BFFFFF; margin: 20px auto; position: relative;}
.detail {margin: 0 0 5px 30px; position: relative; top: 110px;}
.detail input {color: #999; border: 1px solid #74c8e5; border-radius: 3px 3px 3px 3px; height: 15px; line-height: 14px; padding: 8px 5px 7px; width: 184px;}
#suggest {background: none repeat scroll 0 0 #fff; border: 1px solid #ccc; left: 30px; margin: 0; overflow: hidden; padding: 0; position: absolute; text-align: left; top: 142px; visibility: visible; width: 194px; z-index: 1; display: none;}
.note, .item {clear: both; color: #999; cursor: pointer; font-size: 12px; height: 20px; line-height: 20px; list-style: none outside none; margin: 0 1px; padding: 0 5px; white-space: nowrap;}
.active {white-space: nowrap; clear: both; color: rgb(153, 153, 153); cursor: pointer; font-size: 12px; height: 20px; line-height: 20px; list-style: none outside none; margin: 0pt 1px; padding: 0pt 5px; background: none repeat scroll 0% 0% rgb(232, 244, 252);}
</style>
<script>
window.onload = function(){
var s1 = new Suggest();
s1.init(); //初始化程序
}
function Suggest(){
this.oInput = document.getElementById('input1');
this.oUl = document.getElementById('suggest');
this.aLi = this.oUl.getElementsByTagName('li');
}
Suggest.prototype = {
init : function(){
this.toChange();
this.toBlur();
},
toChange : function(){
//ie : onpropertychange
//标准: oninput
var ie = !-[1,];
var This = this;
if(ie){
this.oInput.onpropertychange = function(){
//this.showUl(); this指向不对
if(This.oInput.value == ''){
This.tips();
return;
}
This.showUl();
This.tips();
This.sel(1);
}
} else {
this.oInput.oninput = function(){
This.showUl();
This.tips();
This.sel(1);
}
}
},
showUl : function(){
this.oUl.style.display = 'block';
},
toBlur : function(){
var This = this;
this.oInput.onblur = function(){
This.oUl.style.display = 'none';
}
},
tips : function(){
var value = this.oInput.value;
var re = new RegExp('@' + value.substring(value.indexOf('@')+1) + '');
for(var i=1; i<this.aLi.length; i++){
this.aLi[i].style.display = 'block';
}
if(re.test(value)){
for(var i=1; i<this.aLi.length; i++){
var oEmail = this.aLi[i].getAttribute('email');
if(i==1){
this.aLi[i].innerHTML = value;
} else {
if(re.test(oEmail)){
this.aLi[i].style.display = 'block;'
} else {
this.aLi[i].style.display = 'none';
}
}
}
} else {
for(var i=1; i<this.aLi.length; i++){
var oEmail = this.aLi[i].getAttribute('email');
if(!oEmail){
this.aLi[i].innerHTML = value;
} else {
this.aLi[i].innerHTML = value + oEmail;
}
}
}
},
sel : function(iNow){
var This = this;
for(var i=1; i<this.aLi.length; i++){
this.aLi[i].className = 'item';
}
if(this.oInput.value == ''){
this.aLi[iNow].className = 'item';
} else {
this.aLi[iNow].className = 'active';
}
for(var i=1; i<this.aLi.length; i++){
this.aLi[i].index = i;
this.aLi[i].onmouseover = function(){
for(var i=1; i<This.aLi.length; i++){
This.aLi[i].className = 'item';
}
this.className = 'active';
iNow = this.index;
}
this.aLi[i].onmousedown = function(){
This.oInput.value = this.innerHTML;
}
}
this.oInput.onkeydown = function(ev){
var ev = ev || window.event;
if(ev.keyCode == 38){ //上
if(iNow == 1){
iNow = This.aLi.length-1;
} else {
iNow--;
}
for(var i=1; i<This.aLi.length; i++){
This.aLi[i].className = 'item';
}
This.aLi[iNow].className = 'active';
} else if(ev.keyCode == 40){ //下
if(iNow == This.aLi.length-1){
iNow = 1;
} else {
iNow++;
}
for(var i=1; i<This.aLi.length; i++){
This.aLi[i].className = 'item';
}
This.aLi[iNow].className = 'active';
} else if(ev.keyCode == 13){ //回车
This.oInput.value = This.aLi[iNow].innerHTML;
This.oInput.blur();
}
}
}
}
</script>
</head>
<body>
<div id="login">
<div class="detail">
<input type="text" id="input1" maxlength="128" placeholder="邮箱/会员账号/手机号" autocomplete="off" node-type="loginname" class="name" tabindex="1" name="loginname">
</div>
<div class="detail">
<input type="password" maxlength="24" placeholder="请输入密码" node-type="password" class="pass" tabindex="2" name="password">
</div>
<ul id="suggest">
<li class="note">请选择邮箱类型</li>
<li email="" class="item"></li>
<li email="@sina.com" class="item">@sina.com</li>
<li email="@163.com" class="item">@163.com</li>
<li email="@qq.com" class="item">@qq.com</li>
<li email="@126.com" class="item">@126.com</li>
<li email="@vip.sina.com" class="item">@vip.sina.com</li>
<li email="@sina.cn" class="item">@sina.cn</li>
<li email="@hotmail.com" class="item">@hotmail.com</li>
<li email="@gmail.com" class="item">@gmail.com</li>
<li email="@sohu.com" class="item">@sohu.com</li>
<li email="@yahoo.cn" class="item">@yahoo.cn</li>
<li email="@139.com" class="item">@139.com</li>
<li email="@wo.com.cn" class="item">@wo.com.cn</li>
<li email="@189.cn" class="item">@189.cn</li>
</ul>
</div>
</body>
</html>
基本效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>网页进度条</title>
<style>
#progressBox {width: 300px; height: 40px; border: 1px solid #c8c8c8; background: white; position: relative;}
#progressBar {position: absolute; left: 0; top: 0; z-index: 2; height: 40px; width: 100%; line-height: 40px; color: white; text-align: center; font-size: 20px; font-weight: bold; font-family: Georgia; clip: rect(0px, 0, 40px, 0px); background: #00A1F5;}
#progressText {position: absolute; left: 0; top: 0; z-index: 1; width: 100%; height: 40px; line-height: 40px; color: black; text-align: center; font-size: 20px; font-weight: bold; font-family: Georgia;}
</style>
<script>
window.onload = function(){
var iNow = 0;
var timer = setInterval(function(){
if(iNow == 100){
clearInterval(timer);
} else {
iNow += 2;
progressFn(iNow);
}
}, 30);
function progressFn(cent){
var oDiv1 = document.getElementById('progressBox');
var oDiv2 = document.getElementById('progressBar');
var oDiv3 = document.getElementById('progressText');
var allWidth = parseInt(getStyle(oDiv1, 'width'));
oDiv2.innerHTML = cent + '%';
oDiv3.innerHTML = cent + '%';
oDiv2.style.clip = 'rect(0px, ' + cent/100 * allWidth + 'px, 40px, 0px)';
function getStyle(obj, attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
} else {
return getComputedStyle(obj, false)[attr];
}
}
}
}
</script>
</head>
<body>
<div id="progressBox">
<div id="progressBar">0%</div>
<div id="progressText">0%</div>
</div>
</body>
</html>
反映真实数据的进度条
- 硬编码 : 写死的数据
- 跟flash配合 : as3
- html5 : xhr 2: onprogress事件 onload事件
- 跟后台的配合 : ajax的实时返回数据
硬编码的进度条效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
* {margin: 0; padding: 0;}
#progressBox {width: 300px; height: 40px; border: 1px solid #c8c8c8; background: white; position: relative;}
#progressBar {position: absolute; left: 0; top: 0; z-index: 2; height: 40px; width: 100%; line-height: 40px; color: white; text-align: center; font-size: 20px; font-weight: bold; font-family: Georgia; clip: rect(0px, 0, 40px, 0px); background: #00A1F5;}
#progressText {position: absolute; left: 0; top: 0; z-index: 1; width: 100%; height: 40px; line-height: 40px; color: black; text-align: center; font-size: 20px; font-weight: bold; font-family: Georgia;}
</style>
</head>
<body>
<div id="progressBox">
<div id="progressBar">0%</div>
<div id="progressText">0%</div>
</div>
<div id="div2" style="display: none;">
<img src="http://images.wisegeek.com/young-calico-cat.jpg" >
<img src="http://images.wisegeek.com/lint-roller-against-green-shirt.jpg">
<img src="http://images.wisegeek.com/girl-holding-cat-in-hand-while-blowing-nose.jpg">
<img src="http://images.wisegeek.com/woman-vacuuming.jpg">
<img src="http://images.wisegeek.com/cat-shedding.jpg">
<img src="http://images.wisegeek.com/pet-comb.jpg">
<img src="http://images.wisegeek.com/young-goat-on-a-grass-field.jpg">
</div>
<script>
//硬编码:写死的数据。加载一幅图片就前进1/7的进度
var oDiv2 = document.getElementById('div2');
var aImg = oDiv2.getElementsByTagName('img');
var oDiv1 = document.getElementById('progressBox');
var iNow = 0;
for(var i=0; i<aImg.length; i++){
(function(i){
var yImg = new Image();
yImg.onload = function(){
iNow++;
progressFn(parseInt(iNow/aImg.length * 100));
if(iNow == aImg.length){
oDiv2.style.display = 'block';
oDiv1.style.display = 'none';
}
}
yImg.src = aImg[i].src;
})(i);
}
function progressFn(cent){
var oDiv1 = document.getElementById('progressBox');
var oDiv2 = document.getElementById('progressBar');
var oDiv3 = document.getElementById('progressText');
var allWidth = parseInt(getStyle(oDiv1, 'width'));
oDiv2.innerHTML = cent + '%';
oDiv3.innerHTML = cent + '%';
oDiv2.style.clip = 'rect(0px, ' + cent/100 * allWidth + 'px, 40px, 0px)';
function getStyle(obj, attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
} else {
return getComputedStyle(obj, false)[attr];
}
}
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数码时钟</title>
<style>
body {background: #eee; color: white; font-size: 30px;}
#div1 {width: 300px; height: 36px; border: 1px #fff solid;}
#div1 img {width: 25px; height: 36px; position: relative;}
</style>
<script>
window.onload = function(){
var aImg = document.getElementsByTagName('img');
var oDate = new Date();
var prevStr = toZero(oDate.getHours()) + toZero(oDate.getMinutes()) + toZero(oDate.getSeconds());
var nextStr = '';
var arr = [];
var timer = null;
for(var i=0; i<aImg.length; i++){
aImg[i].src = 'img/' + prevStr.charAt(i) + '.png';
}
setInterval(toChange, 1000);
function toChange(){
var oDate = new Date();
nextStr = toZero(oDate.getHours()) + toZero(oDate.getMinutes()) + toZero(oDate.getSeconds());
/*console.log(prevStr);
console.log(nextStr);*/
toCom(prevStr, nextStr);
prevStr = nextStr;
}
function toCom(str1, str2){
arr = [];
for(var i=0; i<str1.length; i++){
if(str1.charAt(i) != str2.charAt(i)){
arr.push(i);
}
}
startMove();
}
function startMove(){
var iSpeed = -4;
timer = setInterval(function(){
for(var i=0; i<arr.length; i++){
if(aImg[arr[i]].offsetHeight == 0){
iSpeed = 4;
aImg[arr[i]].src = 'img/' + nextStr.charAt(arr[i]) + '.png';
}
aImg[arr[i]].style.height = aImg[arr[i]].offsetHeight + iSpeed + 'px';
aImg[arr[i]].style.top = aImg[arr[i]].offsetHeight/2 - 18 + 'px';
if(aImg[arr[i]].offsetHeight == 36){
clearInterval(timer);
}
}
}, 10)
}
function toZero(num){
if(num < 10){
return '0' + num;
} else {
return '' + num;
}
}
}
</script>
</head>
<body>
<div id="div1">
<img src="img/0.png">
<img src="img/0.png">:
<img src="img/0.png">
<img src="img/0.png">:
<img src="img/0.png">
<img src="img/0.png">
</div>
</body>
</html>
请参照JS分页实例来看
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js分页</title>
<style>
* {margin: 0; padding: 0;}
li {list-style: none;}
#ul1 {width: 600px; height: 250px;}
#ul1 li {width: 100px; height: 100px; background: red; float: left; overflow: hidden; margin: 10px;}
a {margin: 5px;}
</style>
<script src="move.js"></script>
<script>
window.onload = function(){
var json = {
title: [
'效果1',
'效果2',
'效果3',
'效果4',
'效果5',
'效果6',
'效果7',
'效果8',
'效果9',
'效果10',
'效果11',
'效果12',
'效果13',
'效果14',
'效果15',
'效果16',
'效果17',
'效果18',
'效果19',
'效果20',
'效果21',
'效果22',
'效果23',
'效果24',
'效果25',
'效果26',
'效果27',
'效果28',
'效果29',
'效果30',
'效果31',
'效果32',
'效果33',
'效果34'
]
}
var arr = [];
var iNow = 9;
page({
id : 'div1',
nowNum : 1,
allNum : Math.ceil(json.title.length/10),
callBack : function(now, all){
var num = now * 10 < json.title.length ? 10 : json.title.length - (now-1)*10;
var oUl = document.getElementById('ul1');
var aLi = oUl.getElementsByTagName('li');
if(oUl.innerHTML == ''){
for(var i=0; i<num; i++){
var oLi = document.createElement('li');
oLi.innerHTML = json.title[(now-1)*10 + i];
oUl.appendChild(oLi);
}
for(var i=0; i<aLi.length; i++){
arr.push([aLi[i].offsetLeft, aLi[i].offsetTop]);
}
for(var i=0; i<aLi.length; i++){
aLi[i].style.position = 'absolute';
aLi[i].style.left = arr[i][0] + 'px';
aLi[i].style.top = arr[i][1] + 'px';
aLi[i].style.margin = '0'
}
} else {
var timer = setInterval(function(){
startMove(aLi[iNow], {left: 200, top: 250, opacity: 0});
if(iNow == 0){
clearInterval(timer);
iNow = num - 1;
for(var i=0; i<num; i++){
aLi[i].innerHTML = json.title[(now-1)*10 + i];
}
var timer2 = setInterval(function(){
startMove(aLi[iNow], {left: arr[iNow][0], top: arr[iNow][1], opacity: 100});
if(iNow == 0){
clearInterval(timer2);
iNow = num - 1;
} else {
iNow--;
}
}, 100);
} else {
iNow--;
}
}, 100);
}
}
});
}
function page(opt){
if(!opt.id){return false};
var obj = document.getElementById(opt.id);
var nowNum = opt.nowNum || 1;
var allNum = opt.allNum || 5;
var callBack = opt.callBack || function(){};
if(nowNum >= 4 && allNum >=6){
var oA = document.createElement('a');
oA.href = '#1';
oA.innerHTML = '首页';
obj.appendChild(oA);
}
if(nowNum >=2){
var oA = document.createElement('a');
oA.href = '#' + (nowNum -1);
oA.innerHTML = '上一页';
obj.appendChild(oA);
}
if(allNum <= 5){
for(var i=1; i<=allNum; i++){
var oA = document.createElement('a');
oA.href = '#' + i;
if(nowNum == i){
oA.innerHTML = i;
} else {
oA.innerHTML = '[' + i + ']';
}
obj.appendChild(oA);
}
} else {
for(var i=1; i<=5; i++){
var oA = document.createElement('a');
if(nowNum == 1 || nowNum == 2){
oA.href = '#' + i;
if(nowNum == i){
oA.innerHTML = i;
} else {
oA.innerHTML = '[' + i + ']';
}
} else if((allNum - nowNum) == 0 || (allNum - nowNum) == 1 ){
oA.href = '#' + (allNum - 5 + i);
if((allNum - nowNum) == 0 && i == 5){
oA.innerHTML = (allNum - 5 + i);
} else if ((allNum - nowNum) == 1 && i == 4){
oA.innerHTML = (allNum - 5 + i);
} else {
oA.innerHTML = '[' + (allNum - 5 + i) + ']';
}
} else {
oA.href = '#' + (nowNum - 3 + i);
if(i == 3){
oA.innerHTML = nowNum - 3 + i;
} else {
oA.innerHTML = '[' + (nowNum - 3 + i) + ']';
}
}
obj.appendChild(oA);
}
}
if((allNum - nowNum) >= 1){
var oA = document.createElement('a');
oA.href = '#' + (nowNum + 1);
oA.innerHTML = '下一页';
obj.appendChild(oA);
}
if((allNum - nowNum) >= 3 && allNum >= 6){
var oA = document.createElement('a');
oA.href = '#' + allNum;
oA.innerHTML = '尾页';
obj.appendChild(oA);
}
callBack(nowNum, allNum);
var aA = obj.getElementsByTagName('a');
for(var i=0; i<aA.length; i++){
aA[i].onclick = function(){
var nowNum = parseInt(this.getAttribute('href').substring(1));
obj.innerHTML = '';
page({
id : opt.id,
nowNum : nowNum,
allNum : allNum,
callBack : callBack
})
return false; //阻止默认事件,避免#数字添到地址栏
}
}
}
</script>
</head>
<body>
<ul id="ul1"></ul>
<div id="div1">
<!-- <a href="#1">首页</a>
<a href="#3">上一页</a>
<a href="#2">[2]</a>
<a href="#3">[3]</a>
<a href="#4">4</a>
<a href="#5">[5]</a>
<a href="#6">[6]</a>
<a href="#5">下一页</a>
<a href="#10">尾页</a> -->
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>联动下拉菜单</title>
<script>
window.onload = function(){
var s1 = new Sel('div1');
s1.add('0', ['1', '2', '3']);
s1.add('0_0', ['1_1', '1_2', '1_3']);
s1.add('0_0_0', ['1_1_1', '1_1_2', '1_1_3']);
s1.add('0_0_1', ['1_2_1', '1_2_2', '1_2_3']);
s1.add('0_0_2', ['1_3_1', '1_3_2', '1_3_3']);
s1.add('0_1', ['2_1', '2_2', '2_3']);
s1.add('0_1_0', ['2_1_1', '2_1_2', '2_1_3']);
s1.add('0_1_1', ['2_2_1', '2_2_2', '2_2_3']);
s1.add('0_1_2', ['2_3_1', '2_3_2', '2_3_3']);
s1.add('0_2', ['3_1', '3_2', '3_3']);
s1.add('0_2_0', ['3_1_1', '3_1_2', '3_1_3']);
s1.add('0_2_1', ['3_2_1', '3_2_2', '3_2_3']);
s1.add('0_2_2', ['3_3_1', '3_3_2', '3_3_3']);
s1.init(3);
}
function Sel(id){
this.oParent = document.getElementById(id);
this.data = {};
this.aSel = this.oParent.getElementsByTagName('select');
}
Sel.prototype = {
init: function(num){
var This = this;
for(var i=1; i<=num; i++){
var oSel = document.createElement('select');
var oPt = document.createElement('option');
oPt.innerHTML = '默认';
oSel.appendChild(oPt);
oSel.index = i;
this.oParent.appendChild(oSel);
oSel.onchange = function(){
This.change(this.index);
}
}
this.first();
},
add: function(key, value){
this.data[key] = value;
},
first: function(){
var arr = this.data['0'];
for(var i=0; i<arr.length; i++){
var oPt = document.createElement('option');
oPt.innerHTML = arr[i];
this.aSel[0].appendChild(oPt);
}
},
change: function(iNow){
var str = '0';
for(var i=0; i<iNow; i++){
str += '_' + (this.aSel[i].selectedIndex - 1);
}
if(this.data[str]){
var arr = this.data[str];
this.aSel[iNow].options.length = 1;
for(var i=0; i<arr.length; i++){
var oPt = document.createElement('option');
oPt.innerHTML = arr[i];
this.aSel[iNow].appendChild(oPt);
}
this.aSel[iNow].options[1].selected = true;
//以下是个递归
iNow++;
if(iNow < this.aSel.length){
this.change(iNow);
}
} else {
if(iNow < this.aSel.length){
this.aSel[iNow].options.length = 1;
}
}
}
}
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>
简单的数字切换
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>韩雪冬轮播图</title>
<script>
window.onload = function(){
var oDiv = document.getElementById('div1');
var oInput = document.getElementById('input1');
oInput.onclick = function(){
var arr = oDiv.innerHTML.split(',');
arr.push(arr[0]);
arr.shift();
oDiv.innerHTML = arr;
}
}
</script>
</head>
<body>
<div id="div1">1,2,3,4</div>
<input type="button" value="切换" id="input1">
</body>
</html>
三个div进行切换
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>韩雪冬轮播图</title>
<style>
.box1 {width: 100px; height: 100px; background: red; position: absolute; left: 100px; top: 100px;}
.box2 {width: 100px; height: 100px; background: yellow; position: absolute; left: 250px; top: 50px;}
.box3 {width: 100px; height: 100px; background: blue; position: absolute; left: 400px; top: 100px;}
</style>
<script>
window.onload = function(){
var aInput = document.getElementsByTagName('input');
var aDiv = document.getElementsByTagName('div');
var arr = [];
for(var i=0; i<aDiv.length; i++){
arr.push([getStyle(aDiv[i], 'left'), getStyle(aDiv[i], 'top')]);
}
// console.log(arr);
aInput[0].onclick = function(){
arr.push(arr[0]);
arr.shift();
for(var i=0; i<aDiv.length; i++){
aDiv[i].style.left = arr[i][0];
aDiv[i].style.top = arr[i][1];
}
}
aInput[1].onclick = function(){
arr.unshift(arr[arr.length-1]);
arr.pop();
for(var i=0; i<aDiv.length; i++){
aDiv[i].style.left = arr[i][0];
aDiv[i].style.top = arr[i][1];
}
}
function getStyle(obj, attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
} else {
return getComputedStyle(obj, false)[attr];
}
}
}
</script>
</head>
<body>
<input type="button" value="←">
<input type="button" value="→">
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>
妙味官网的演示效果
<!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>韩雪冬网站效果 - 妙味课堂 - www.miaov.com</title>
<!--[if lte IE 6]>
<script src="js/DD_belatedPNG_0.0.8a.js" type="text/javascript"></script>
<script type="text/javascript">
DD_belatedPNG.fix('span');
</script>
<![endif]-->
<script type="text/javascript" src="miaov.js"></script>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="automatic">
<div class="prev_div"></div>
<a class="prev" href="###">
<span class="ico1"></span>
<span class="ico"></span>
<span class="txt"></span>
</a>
<div class="next_div"></div>
<a class="next" href="###">
<span class="ico1"></span>
<span class="ico"></span>
<span class="txt"></span>
</a>
<div class="line"></div>
<ul>
<li class="pos_0"><a href="http://www.miaov.com"><img src="images/8.jpg" width="100" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
<li class="pos_1"><a href="http://www.miaov.com"><img src="images/1.jpg" width="270" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
<li class="pos_2"><a href="http://www.miaov.com"><img src="images/2.jpg" width="510" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
<li class="pos_3"><a href="http://www.miaov.com"><img src="images/3.jpg" width="680" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
<li class="pos_4"><a href="http://www.miaov.com"><img src="images/4.jpg" width="510" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
<li class="pos_5"><a href="http://www.miaov.com"><img src="images/5.jpg" width="270" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
<li class="pos_6"><a href="http://www.miaov.com"><img src="images/6.jpg" width="270" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
<li class="pos_6"><a href="http://www.miaov.com"><img src="images/7.jpg" width="270" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
</ul>
</div>
<p id="footer"><a href="http://www.miaov.com">妙味课堂 - www.miaov.com</a></p>
</body>
</html>
@charset "utf-8";
/* CSS Document */
* { padding: 0; margin: 0; }
li { list-style: none; }
img { border: none; }
body { background: #ececec; padding-top: 50px; }
#automatic { width: 970px; height: 344px; position: relative; margin: 0 auto; overflow: hidden; }
.prev_div { width: 130px; height: 72px; position: absolute; top: 128px; left: 92px; z-index: 5; background: red; filter: alpha(opacity=0); opacity: 0; cursor: pointer; }
.next_div { width: 130px; height: 72px; position: absolute; top: 128px; right: 92px; z-index: 5; background: red; filter: alpha(opacity=0); opacity: 0; cursor: pointer; }
#automatic .prev { width: 120px; height: 72px; position: absolute; top: 108px; left: 72px; z-index: 4; }
#automatic .prev .ico { width: 76px; height: 112px; position: absolute; top: 0; left: 0; background: url(images/prev.png); }
#automatic .prev .ico1 { width: 76px; height: 112px; position: absolute; top: 0; left: 0; background: url(images/prev_1.png); z-index: 2; filter: alpha(opacity=0); opacity: 0; }
#automatic .prev .txt { width: 106px; height: 112px; position: absolute; top: 0; left: 65px; background: url(images/prev_txt.png) no-repeat; filter: alpha(opacity=0); opacity: 0; }
#automatic .next { width: 120px; height: 72px; position: absolute; top: 108px; right: 72px; z-index: 4; }
#automatic .next .ico { width: 76px; height: 112px; position: absolute; top: 0; right: 0; background: url(images/next.png) no-repeat; }
#automatic .next .ico1 { width: 76px; height: 112px; position: absolute; top: 0; right: 0px; background: url(images/next_1.png) no-repeat; z-index: 2; filter: alpha(opacity=0); opacity: 0; }
#automatic .next .txt { width: 106px; height: 112px; position: absolute; top: 0; right: 65px; background: url(images/next_txt.png) no-repeat; filter: alpha(opacity=0); opacity: 0; }
#automatic ul { width: 970px; height: 344px; position: absolute; top: 0; left: 0; z-index: 1; }
#automatic li { position: absolute; }
#automatic .line { border: 4px solid #fff; width: 672px; height: 336px; position: absolute; top: 0; left: 50%; margin-left: -340px; z-index: 3; }
#automatic .pos_0 { top: -104px; left: 0; z-index: 1; filter: alpha(opacity=0); opacity: 0; }
#automatic .pos_1 { top: 104px; left: 0; z-index: 2; filter: alpha(opacity=60); opacity: 0.6; }
#automatic .pos_2 { top: 43px; left: 50px; z-index: 3; filter: alpha(opacity=80); opacity: 0.8; }
#automatic .pos_3 { top: 0; left: 145px; z-index: 4; }
#automatic .pos_4 { top: 43px; right: 50px; z-index: 3; filter: alpha(opacity=80); opacity: 0.8; }
#automatic .pos_5 { top: 104px; right: 0; z-index: 2; filter: alpha(opacity=60); opacity: 0.6; }
#automatic .pos_6 { top: -104px; right: 0; z-index: 1; filter: alpha(opacity=0); opacity: 0; }
#footer { width: 970px; height: 30px; text-align: center; padding-top: 50px; margin: 0 auto; }
#footer a { color: #930; font-family: arial; font-size: 15px; text-decoration: none; border-bottom: 1px dotted #930; }
#footer a:hover { border-bottom: 1px solid #930; }
function getElementsByClassName(oParent, sClass)
{
var aTmp=oParent.getElementsByTagName('*');
var aResult=[];
var i=0;
for(i=0;i<aTmp.length;i++)
{
if(aTmp[i].className==sClass)
{
aResult.push(aTmp[i]);
}
}
return aResult;
}
window.onload=function ()
{
var oDiv=document.getElementById('automatic');
var oPrevMask=getElementsByClassName(oDiv,'prev_div')[0];
var oNextMask=getElementsByClassName(oDiv,'next_div')[0];
var oPrevBtn=getElementsByClassName(oDiv,'prev')[0];
var oNextBtn=getElementsByClassName(oDiv,'next')[0];
var oPrevArrow=getElementsByClassName(oPrevBtn,'ico')[0];
var oPrevArrowLight=getElementsByClassName(oPrevBtn,'ico1')[0];
var oPrevTxt=getElementsByClassName(oPrevBtn,'txt')[0];
var oNextArrow=getElementsByClassName(oNextBtn,'ico')[0];
var oNextArrowLight=getElementsByClassName(oNextBtn,'ico1')[0];
var oNextTxt=getElementsByClassName(oNextBtn,'txt')[0];
oPrevArrow.alpha=100;
var iInitPrevArrow=oPrevArrow.left=oPrevArrow.offsetLeft;
oPrevArrowLight.alpha=0;
var iInitPrevArrowLight=oPrevArrowLight.left=oPrevArrowLight.offsetLeft;
oPrevTxt.alpha=0;
var iInitPrevTxt=oPrevTxt.left=oPrevTxt.offsetLeft;
oNextArrow.alpha=100;
var iInitNextArrow=oNextArrow.left=oNextArrow.offsetLeft;
oNextArrowLight.alpha=0;
var iInitNextArrowLight=oNextArrowLight.left=oNextArrowLight.offsetLeft;
oNextTxt.alpha=0;
var iInitNextTxt=oNextTxt.left=oNextTxt.offsetLeft;
var aLi=oDiv.getElementsByTagName('ul')[0].getElementsByTagName('li');
var aLiInit=[];
var oLine=getElementsByClassName(oDiv, 'line')[0];
var iInterval=150;
//var bIsIE=(/msie/i).test(window.navigator.userAgent);
var i=0;
for(i=0;i<aLi.length;i++)
{
aLiInit[i]={};
aLi[i].width=aLiInit[i].w=aLi[i].getElementsByTagName('img')[0].offsetWidth;
aLi[i].height=aLiInit[i].h=aLi[i].getElementsByTagName('img')[0].offsetHeight;
aLi[i].left=aLiInit[i].l=aLi[i].offsetLeft;
aLi[i].top=aLiInit[i].t=aLi[i].offsetTop;
aLi[i].alpha=aLiInit[i].alpha=0;
aLi[i].z=aLiInit[i].z=1;
}
for(i=0;i<aLi.length;i++)
{
aLi[i].style.position='absolute';
aLi[i].style.left=aLiInit[i].l+'px';
aLi[i].style.top=aLiInit[i].t+'px';
}
aLi[1].alpha=aLiInit[1].alpha=60;
aLi[2].alpha=aLiInit[2].alpha=80;
aLi[3].alpha=aLiInit[3].alpha=100;
aLi[4].alpha=aLiInit[4].alpha=80;
aLi[5].alpha=aLiInit[5].alpha=60;
aLi[1].z=aLiInit[1].z=2;
aLi[2].z=aLiInit[2].z=3;
aLi[3].z=aLiInit[3].z=4;
aLi[4].z=aLiInit[4].z=3;
aLi[5].z=aLiInit[5].z=2;
/*if(bIsIE)
{
oPrevArrowLight.style.display='none';
oPrevArrow.style.display='block';
}*/
oPrevMask.onmouseover=function ()
{
/*if(bIsIE)
{
startMove(oPrevArrow, {left: iInitPrevArrow+10}, iInterval);
startMove(oPrevTxt, {left: iInitPrevTxt-10, alpha:100}, iInterval);
}
else
{
startMove(oPrevArrow, {left: iInitPrevArrow+10}, iInterval);
startMove(oPrevArrowLight, {left: iInitPrevArrowLight+10, alpha:100}, iInterval);
startMove(oPrevTxt, {left: iInitPrevTxt-10, alpha:100}, iInterval);
}*/
startMove(oPrevArrow, {left: iInitPrevArrow+10}, iInterval);
startMove(oPrevArrowLight, {left: iInitPrevArrowLight+10, alpha:100}, iInterval);
startMove(oPrevTxt, {left: iInitPrevTxt-10, alpha:100}, iInterval);
};
oPrevMask.onmouseout=function ()
{
/*if(bIsIE)
{
startMove(oPrevArrow, {left: iInitPrevArrow}, iInterval);
startMove(oPrevTxt, {left: iInitPrevTxt, alpha:0}, iInterval);
}
else
{
startMove(oPrevArrow, {left: iInitPrevArrow}, iInterval);
startMove(oPrevArrowLight, {left: iInitPrevArrowLight, alpha:0}, iInterval);
startMove(oPrevTxt, {left: iInitPrevTxt, alpha:0}, iInterval);
}*/
startMove(oPrevArrow, {left: iInitPrevArrow}, iInterval);
startMove(oPrevArrowLight, {left: iInitPrevArrowLight, alpha:0}, iInterval);
startMove(oPrevTxt, {left: iInitPrevTxt, alpha:0}, iInterval);
};
oPrevMask.onmousedown=function ()
{
gotoImg(true);
};
oNextMask.onmouseover=function ()
{
startMove(oNextArrow, {left: iInitNextArrow-10}, iInterval);
startMove(oNextArrowLight, {left: iInitNextArrowLight-10, alpha:100}, iInterval);
startMove(oNextTxt, {left: iInitNextTxt+10, alpha:100}, iInterval);
};
oNextMask.onmouseout=function ()
{
startMove(oNextArrow, {left: iInitNextArrow}, iInterval);
startMove(oNextArrowLight, {left: iInitNextArrowLight, alpha:0}, iInterval);
startMove(oNextTxt, {left: iInitNextTxt, alpha:0}, iInterval);
};
oNextMask.onmousedown=function ()
{
gotoImg(false);
};
function gotoImg(bLeft)
{
if(bLeft)
{
aLiInit.push(aLiInit.shift());
}
else
{
aLiInit.unshift(aLiInit.pop());
}
oLine.style.display='none';
for(i=0;i<aLi.length;i++)
{
startMove(aLi[i], {left: aLiInit[i].l, top: aLiInit[i].t, width: aLiInit[i].w, height:aLiInit[i].h, alpha:aLiInit[i].alpha, zIndex:aLiInit[i].z}, 300, function (){oLine.style.display='block';});
}
};
};
function startMove(obj, oParams, iTime, fnCallBackEnd)
{
var iInterval=45;
var iEndTime=(new Date()).getTime()+iTime;
var iTimes=Math.ceil(iTime/iInterval);
var oSpeed={};
if(typeof obj.timer=='undefined')
{
obj.timer=null;
}
for(key in oParams)
{
oSpeed[key]=(oParams[key]-obj[key])/iTimes;
}
if(obj.timer)
{
clearInterval(obj.timer);
}
obj.timer=setInterval
(
function ()
{
doMove(obj, oParams, oSpeed, iEndTime, fnCallBackEnd);
}, iInterval
);
}
function doMove(obj, oTarget, oSpeed, iEndTime, fnCallBackEnd)
{
var iNow=(new Date()).getTime();
if(iNow>=iEndTime)
{
clearInterval(obj.timer);
obj.timer=null;
for(key in oTarget)
{
obj[key]=oTarget[key];
// alert('set '+key+'='+oTarget[key]);
switch(key)
{
case 'alpha':
obj.style.opacity=oTarget[key]/100;
obj.style.filter="alpha(opacity:"+oTarget[key]+")";
break;
case 'zIndex':
obj.style.zIndex=oTarget[key];
break;
case 'width':
case 'height':
obj.getElementsByTagName('img')[0].style[key]=oTarget[key]+'px';
break;
default:
obj.style[key]=oTarget[key]+'px';
break;
}
}
if(fnCallBackEnd)
{
fnCallBackEnd();
}
}
else
{
for(key in oTarget)
{
obj[key]+=oSpeed[key];
switch(key)
{
case 'alpha':
obj.style.opacity=obj[key]/100;
obj.style.filter="alpha(opacity:"+obj[key]+")";
break;
case 'zIndex':
//obj.style.zIndex=obj[key];
obj.style.zIndex=oTarget[key];
break;
case 'width':
case 'height':
obj.getElementsByTagName('img')[0].style[key]=obj[key]+'px';
break;
default:
obj.style[key]=obj[key]+'px';
break;
}
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>华为全屏轮播图</title>
<style>
* {padding: 0; margin: 0;}
li {list-style: none;}
#div1 {min-width: 1000px; height: 296px; position: relative; overflow: hidden;}
#div1 ul {position: absolute; left; 0}
#div1 ul li {float: left;}
#div1 ul li img {position: relative;}
#btn {position: absolute; width: 100%; text-align: center; bottom: 0;}
#btn a {cursor: pointer; display: inline-block; width: 11px; height: 11px; background: #666;}
#btn a.active {background: white;}
#btn a:hover {background: white;}
/*min-width属性ie不支持,下面是一个hack*/
*html .ie6_out {margin-left: 1000px; zoom: 1;}
*html .ie6_in {position: relative; float: left; margin-left: -1000px;}
</style>
<script src="move.js"></script>
<script>
window.onload = function(){
var oDiv = document.getElementById('div1');
var oUl = oDiv.getElementsByTagName('ul')[0];
var aLi = oUl.getElementsByTagName('li');
var aImg = oUl.getElementsByTagName('img');
var oBtn = document.getElementById('btn');
var aA = oBtn.getElementsByTagName('a');
var imgWidth = 1680;
var iNow = 0;
var iNow2 = 0;
oUl.style.width = aImg.length * imgWidth + 'px';
function toReSize(){
var veiwWidth = document.documentElement.clientWidth;
if(veiwWidth>1000){
for(var i=0;i<aImg.length;i++){
aImg[i].style.left = - (imgWidth - veiwWidth)/2 + 'px';
}
}
}
toReSize();
window.onresize = function(){
toReSize();
};
for(var i=0;i<aA.length;i++){
aA[i].index = i;
aA[i].onclick = function(){
for(var i=0;i<aA.length;i++){
aA[i].className = '';
}
this.className = 'active';
startMove(oUl,{left : - this.index * imgWidth});
};
}
//setInterval(toRun,3000);
var timer = null;
var bBtn = true;
window.onblur=function(){
if(bBtn){
clearInterval(timer);
bBtn = false;
}
};
window.onfocus=function(){
if(!bBtn){
timer = setInterval(toRun,3000);
bBtn = true;
}
};
timer = setInterval(toRun,3000);
function toRun(){
if(iNow == aLi.length-1){
aLi[0].style.position = 'relative';
aLi[0].style.left = aLi.length * imgWidth + 'px';
iNow = 0;
}
else{
iNow++;
}
iNow2++;
for(var i=0;i<aA.length;i++){
aA[i].className = '';
}
aA[iNow].className = 'active';
startMove(oUl,{left : - iNow2 * imgWidth},function(){
if(iNow==0){
aLi[0].style.position = 'static';
oUl.style.left = 0;
iNow2 = 0;
}
});
}
};
</script>
</head>
<body>
<!--[if lte IE 6]>
<div class="ie6_out">
<div class="ie6_in">
<![endif]-->
<div id="div1">
<ul>
<li><img src="01.jpg"></li>
<li><img src="02.jpg"></li>
<li><img src="03.jpg"></li>
</ul>
<div id="btn">
<a href="javascript:;" class="active"></a>
<a href="javascript:;"></a>
<a href="javascript:;"></a>
</div>
</div>
<!--[if lte IE 6]>
</div>
</div>
<![endif]-->
</body>
</html>
style.js文件
function getBroswerType()
{
var Sys = {};
var ua = navigator.userAgent.toLowerCase();
var s;
(s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] :
(s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
(s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
(s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
(s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;
return Sys;
}
function getCss(obj, attr)
{
if(attr=="rotate")
{
return obj.rotate;
}
var i=parseFloat(obj.currentStyle?obj.currentStyle[attr]:document.defaultView.getComputedStyle(obj, false)[attr]);
var val=i?i:0;
if(attr=="opacity")
{
val*=100;
}
return val;
}
function setCss(obj,oAttr)
{
var sAttr="";
var arr=["Webkit","Moz","O","ms",""];
for(sAttr in oAttr)
{
if(sAttr.charAt(0)=="$")
{
for(var i=0;i<arr.length;i++)
{
obj.style[arr[i]+sAttr.substring(1)]=oAttr[sAttr];
}
}
else if(sAttr=="rotate")
{
obj.rotate=oAttr[sAttr];
var a=Math.cos(obj.rotate/180*Math.PI);
var b=Math.sin(obj.rotate/180*Math.PI);
var c=-Math.sin(obj.rotate/180*Math.PI);
var d=Math.cos(obj.rotate/180*Math.PI);
for(var i=0;i<arr.length;i++)
{
obj.style[arr[i]+"Transform"]="matrix("+a+","+b+","+c+","+d+","+0+","+0+")";
}
obj.style.filter="progid:DXImageTransform.Microsoft.Matrix( M11="+a+", M12="+c+", M21="+b+", M22="+d+",SizingMethod='auto expand')";
}
else
{
var value=oAttr[sAttr];
switch(sAttr)
{
case 'width':
case 'height':
case 'paddingLeft':
case 'paddingTop':
case 'paddingRight':
case 'paddingBottom':
value=Math.max(value,0);
case 'left':
case 'top':
case 'marginLeft':
case 'marginTop':
case 'marginRight':
case 'marginBottom':
obj.style[sAttr]=value+'px';
break;
case 'opacity':
if(value<0)
{
value=0;
}
obj.style.filter="alpha(opacity:"+value+")";
obj.style.opacity=value/100;
break;
default:
obj.style[sAttr]=value;
}
}
}
}
html代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>仿苹果系统文件倒影展示效果-妙味课堂-www.miaov.com</title>
<style>
html{height:100%;background:#000;}
body{margin:0;height:100%;overflow:hidden;}
#wrap{ height:500px;width:1024px; position:absolute;left:50%;top:50%;margin:-250px 0 0 -512px;}
#imgList{ position:absolute;}
h1,p { text-align:center; color:#f1f1f1; word-spacing:15px; position:relative; top:10px; z-index:2; }
h1 a,p a { color:#f1f1f1; text-decoration:none; }
h1 a:hover,p a:hover { color:#fff; border-bottom:1px dotted #fff; }
</style>
<script src="js/style.js"></script>
<script>
window.onload=function()
{
var oBox=document.getElementById("wrap");
var oList=document.getElementById("imgList");
var aImg=oList.getElementsByTagName("img");
var aDiv=[];
var iImgWidth=300;
var iInterval=100;
var iStarWidth=1024;
var iStarHeight=500;
var iHeight=0;
var iLeft=-20;
var iZindex=0;
var iStarNow=0;
var iDeg=45;
var iNow=0;
var iGap=200;
var sHtml="";
var aSpan=[];
setCss(oBox,{$Perspective:"800px"});
for(var i=0; i<aImg.length;i++)
{
setCss(aImg[i],{width:iImgWidth});
sHtml+="<div style='width:"+aImg[i].offsetWidth+"px;height:"
+aImg[i].offsetHeight+"px;background:url("+aImg[i].src+") no-repeat; background-size:100% 100%;display:none; position:absolute;bottom:0;'><span style='width:"+aImg[i].offsetWidth+"px;height:"
+aImg[i].offsetHeight+"px;background:-webkit-linear-gradient(top, rgba(0, 0, 0, 1), rgba(0, 0, 0, 0.3)),url("+aImg[i].src+") no-repeat;background:-moz-linear-gradient(top, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.6)),url("+aImg[i].src+") no-repeat;background:-o-linear-gradient(top, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.6)),url("+aImg[i].src+") no-repeat;background:linear-gradient(top, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.6)),url("+aImg[i].src+") no-repeat;background-size:100% 100%,100% 100%;position:absolute;'></span></div>";
}
oList.innerHTML="";
oList.innerHTML=sHtml;
aDiv=oList.getElementsByTagName("div");
aSpan=oList.getElementsByTagName("span");
for(i=0;i<aDiv.length;i++)
{
iLeft+=iInterval;
if(i<Math.floor(aDiv.length/2))
{
iZindex=i;
setCss(aDiv[i],{$Transform:"translateZ(-200px) rotateY("+iDeg+"deg)",left:iLeft,zIndex:iZindex,display:"block",$TransformStyle:"preserve-3d"});
}
else if(i>Math.floor(aDiv.length/2))
{
iZindex=aImg.length-i;
setCss(aDiv[i],{$Transform:"translateZ(-200px) rotateY("+-iDeg+"deg)",left:iLeft,zIndex:iZindex,display:"block",$TransformStyle:"preserve-3d"});
}
else
{
iZindex=i;
iLeft+=iGap;
setCss(aDiv[i],{$Transform:"translateZ(200px)",left:iLeft,zIndex:iZindex,display:"block",$TransformStyle:"preserve-3d"});
setCss(oList,{left:(oBox.offsetWidth/2-(iLeft+iImgWidth/2))});
iStarNow=iNow=i;
iLeft+=iGap;
}
setCss(aSpan[i],{$TransformOrigin:"bottom",$Transform:"rotateX(180deg)"})
iHeight=Math.max(iHeight,aDiv[i].offsetHeight);
aDiv[i].index=i;
aDiv[i].onclick=function()
{
if(this.index==iNow)
{
return;
}
iLeft=(iStarNow-this.index)*iInterval;
iNow=this.index;
tab(aDiv,iNow,iLeft,iInterval,iGap);
}
}
setCss(oList,{height:iHeight,top:oBox.offsetHeight/2-iHeight/2,$TransformStyle:"preserve-3d"});
window.onresize=resize;
function resize()
{
var iWidth=document.documentElement.clientWidth;
var iHeight=document.documentElement.clientHeight;
setCss(oBox,{$Transform:"scale("+Math.min(iWidth/iStarWidth,iHeight/iStarHeight)+")"});
};
resize();
};
function tab(aImg,iNow,iLeft,iInterval,iGap)
{
var iImgWidth=300;
var iInterval=100;
var iZindex=0;
var iDeg=45;
for(var i=0;i<aImg.length;i++)
{
setCss(aImg[i],{$Transition:"0.7s all ease"});
iLeft+=iInterval;
if(i<iNow)
{
iZindex=i;
setCss(aImg[i],{$Transform:"translateZ(-200px) rotateY("+iDeg+"deg)",left:iLeft,zIndex:iZindex});
}
else if(i>iNow)
{
iZindex=aImg.length-i;
setCss(aImg[i],{left:iLeft,$Transform:"translateZ(-200px) rotateY("+-iDeg+"deg)",zIndex:iZindex});
}
else
{
iZindex=i;
iLeft+=iGap;
setCss(aImg[i],{width:iImgWidth,$Transform:"translateZ(200px) rotateY(0deg)",left:iLeft,zIndex:iZindex});
iLeft+=iGap;
}
}
}
</script>
</head>
<body>
<h1><a href="http://www.miaov.com/">妙味课堂</a>——苹果系统文件倒影展示效果</h1>
<p><a href="http://bbs.miaov.com/forum.php?mod=viewthread&tid=6277">>>源文件下载、交流讨论</a></p>
<div id="wrap">
<div id="imgList">
<img src="img/1.jpg" alt=""/>
<img src="img/2.jpg" alt=""/>
<img src="img/3.jpg" alt=""/>
<img src="img/4.jpg" alt=""/>
<img src="img/5.jpg" alt=""/>
<img src="img/6.jpg" alt=""/>
<img src="img/7.jpg" alt=""/>
<img src="img/8.jpg" alt=""/>
<img src="img/9.jpg" alt=""/>
<img src="img/10.jpg" alt=""/>
<img src="img/11.jpg" alt=""/>
<img src="img/1.jpg" alt=""/>
<img src="img/2.jpg" alt=""/>
<img src="img/3.jpg" alt=""/>
<img src="img/4.jpg" alt=""/>
<img src="img/5.jpg" alt=""/>
<img src="img/6.jpg" alt=""/>
<img src="img/7.jpg" alt=""/>
<img src="img/8.jpg" alt=""/>
<img src="img/9.jpg" alt=""/>
<img src="img/10.jpg" alt=""/>
<img src="img/11.jpg" alt=""/>
</div>
</div>
</body>
<script>
if(getBroswerType().ie || getBroswerType().opera || parseFloat(getBroswerType().chrome)<12 || parseFloat(getBroswerType().firefox)<12 || parseFloat(getBroswerType().safari)<4)
{
toMiaoV();
}
function toMiaoV()
{
document.body.innerHTML="对不起,您的浏览器版本过低请升级,建议使用chrome 12.0(或firefox 12.0 、safari 4.0)以上浏览器浏览本效果";
with(document.body.style)
{
textAlign="center";
padding="0 100px"
font="bold 50px/150px '微软雅黑'";
color="#fff";
}
setTimeout(function()
{
window.location.href="http://www.miaov.com";
},5000);
}
</script>
</html>