2.js按鈕特效大全
阿新 • • 發佈:2019-02-14
//2.1頁面重新整理按鈕
//重點:重新整理頁面的方法 reload 如果要使用meta元素 使用refresh屬性
function renovates(){
document.location.reload();
}
//2.2按Enter鍵呼叫登入按鈕
function keyLogin(){
if(event.keyCode==13){//按鍵enter的鍵值為13
document.getElementById("input1").click();//呼叫登入按鈕
}
}
//2.3動態建立按鈕 提高頁面的訪問速度
//重點:createElement(),appendChild()
var i=0;
function addInput(){
var o=document.createElement("input");//建立DOM
o.type="button";//設定型別
o.value="按鈕"+i++;//設定屬性
o.attachEvent("onclick",addInput);//新增事件
document.body.appendChild(o);//新增到主窗體
o=null;//釋放物件
}
//2.9獲取按鈕的絕對位置
//使用this作為當前引數傳遞,讀取offsetTop/Left屬性
function getAddress(e){
var t=e.offsetTop;
var l=e.offsetLeft
while(e=e.offsetParent){
t+=e.offsetTop;//取x座標
l+=e.offsetLeft;//取y座標
}
alert("x座標"+x+"y座標"+l)
}
//2.10刪除時確認提示
function del(){
if(confirm('確認要刪除嗎')){
alert('已經刪除');
}else{
alert('已經取消刪除操作');
}
}
//2.11防止按鈕連擊 重複提交資料
//重點:如何判斷頁面的狀態readyState屬性
function doubleCheck(){
if(window.document.readyState!=null&&window.document.readyState!='complete'){//判斷頁面是否執行完
alert("正在處理,請稍等!");
return false;
}else{
return true;
}
}
//2.12圖片式的按鈕
//重點:selectedIndex屬性,獲取使用者的選擇,然後指定導航地址location
function goTo(){
var myindex=document.myform.mailBox.selectedIndex;//獲取下拉選中的索引
location=document.myform.mailBox.options[myindex].value;//獲取下拉框的選擇值
}
//2.14選擇不同的列表項時候顯示不同的按鈕
//重點:innerHTML
function butSelect(){
var selVal=document.getElementById("sel").value;
if(selVal=="1"){
document.getElementById("td").innerHTML="<input type='button' value='按鈕1' onclick='btnc1();' />";
}else if(selVal=="2"){
document.getElementById("td").innerHTML="<input type='button' value='按鈕2' onclick='btnc2();' />";
}else{
document.getElementById('td').innerHTML='';
}
}
function btnc1(){
alert("單擊了按鈕1");
}
function btnc2(){
alert('單擊了按鈕2');
}
//2.15使用按鈕控制文字漸變
//重點:使用eval將普通字串連成函式名
var x=9;
var change='on';
if(navigator.appName=='Netscape'){//瀏覽器是NetScape
visShow="'show'";//顯示
visHide="'hide'";//隱藏
docStyle="document.";
styleDoc="";
}else{//瀏覽器是IE
visHide="'hidden'";//顯示
visShow="'visible'";//隱藏
docStyle="";
styleDoc=".style";//設定樣式
}
function hide1(){
eval(docStyle+'object1'+styleDoc+'.visibility='+visHide);
}
function hide2(){
eval(docStyle+'object2'+styleDoc+'.visibility='+visHide);
}
function hide3(){
eval(docStyle+'object3'+styleDoc+'.visibility='+visHide);
}
function show1(){
eval(docStyle+'object1'+styleDoc+'.visibility='+visShow);
}
function show2(){
eval(docStyle+'object2'+styleDoc+'.visibility='+visShow);
}
function show3(){
eval(docStyle+'object3'+styleDoc+'.visibility='+visShow);
}
function change1(){
x+=1;
eval("show"+x+"()");
if(x<2){
setTimeout("change1()",75);
}else{
change2();
}
}
function change2(){
x-=1;
eval("show"+x+"()");
if(x>1){
setTimeout("change2()",75);
}else{
change1();
}
}
function changeOn(){//開始變化的控制
x=2;
change="on";
change1();
}
function changeOff(){//結束變化的控制
change="off";
}
//2.16大翻頁效果的公告欄
var Scroll=new Function(){
this.delay=2000;//延遲時間
this.height=20;//行高
this.step=4;//步長
this.curHeight=0;
this.stimer=nll;
this.timer=null;
this.start=function(){//開始翻頁
this.move();
}
this.move=function(){
var self=this;
if(this.curHeight==this.height){//顯示完一行
this.timer=setTimeout(function(){self.move()},this.delay);//使用定時器
this.height=0;
if(this.element.scrollTop>=this.element.scrollHeight-this.height){
this.element.scrollTop=0;//瀏覽器資訊完成
}
return true;
}
this.element.scrollTop+=this.step;
this.curHeight=setTimeout(function(){self.move()},30);
}
this.stop=function(){//清除定時器
clearTimeout(this.timer);
}
}
//2.17動態設定控制元件的事件
//為控制元件繫結事件
function addClick(obj){
obj.onclick=function(){
alert('動態新增事件成功');
}
}