JS實(shí)現(xiàn)懸浮移動(dòng)窗口(懸浮廣告)的特效
js方法:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script type="text/javascript">
window.onload=function(){
//寫入
var oneInner = document.createElement("div");
oneInner.setAttribute("style","background:#663398;position:absolute;width:100px;height:100px;border:solid 3px #2F74A7;cursor:pointer;");
var oneButton = document.createElement("input");
oneButton.setAttribute("type","button");
oneButton.setAttribute("value","x");
if (oneButton.style.cssFloat)
{
oneButton.style.cssFloat="right"
}
else
{oneButton.style.styleFloat="right"}
oneInner.appendChild(oneButton);
document.body.appendChild(oneInner);
//定時(shí)器
var a1a = setInterval(moves,100);
//函數(shù)
var x = 10;
var y = 10;
function moves(){
var tops = oneInner.offsetTop
var lefts = oneInner.offsetLeft
if (lefts>=document.documentElement.clientWidth-oneInner.offsetWidth||lefts<=0)
{
x=-x
}
if (tops>=document.documentElement.clientHeight-oneInner.offsetHeight||tops<=0)
{
y=-y
}
tops+=y;
lefts+=x;
oneInner.style.top=tops+"px"
oneInner.style.left=lefts+"px"
}
//懸停停止
oneInner.onmouseover=function(){
clearInterval(a1a);
}
//放手繼續(xù)運(yùn)動(dòng)
oneInner.onmouseout=function(){
a1a =setInterval(moves,100);
}
//刪除
oneButton.onclick=function(){
document.body.removeChild(oneInner);
}
}
</script>
</head>
<body>
</body>
</html>
jquery方法:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="http://www.dbjr.com.cn/workspace/js/jquery-1.7.min.js"></script>
<script>
$(function(){
//寫入div
$("<div/>",{id:"moveWindow"}).css({width:"200px",height:"200px",border:"solid 3px #2F74A7",background:"#663398",position:"absolute",cursor:"pointer"}).appendTo("body");
//寫入關(guān)閉按鈕
$("<div/>",{id:"removeMW"}).css({width:"20px",height:"20px",background:"red",float:"right"}).appendTo("#moveWindow");
//定時(shí)器
var move = setInterval(moves,100);
var x= 10;
var y= 10;
function moves (){
var mw = $("#moveWindow").offset();
var lefts =mw.left;
var tops = mw.top;
if (lefts>=$(window).width()-$("#moveWindow").width()||lefts<=0)
{
x=-x
}
if (tops>=$(window).height()-$("#moveWindow").height()||tops<=0)
{
y=-y
}
lefts+=x;
tops+=y;
$("#moveWindow").offset({top:tops,left:lefts});
}
//懸停停止運(yùn)動(dòng)
$("#moveWindow").mouseover(function(){
clearInterval(move);
});
//移開鼠標(biāo)后繼續(xù)運(yùn)動(dòng)
$("#moveWindow").mouseout(function(){
move = setInterval(moves,100);
});
//點(diǎn)擊按鈕關(guān)閉
$("#removeMW").click(function(){
$("#moveWindow").remove();
});
})
</script>
</head>
<body>
</body>
</html>
基本思路:
1.頁面加載完成之后向頁面插入窗口,之后向窗口插入關(guān)閉按鈕
2.使用setInterval()函數(shù)觸發(fā)moves()函數(shù)開始動(dòng)畫
3.moves函數(shù):首先獲取當(dāng)前窗口位置,之后隨時(shí)間使窗口位移,每當(dāng)位移到游覽器邊緣時(shí)反向運(yùn)動(dòng)
4.添加其他事件:鼠標(biāo)懸停停止運(yùn)動(dòng),鼠標(biāo)離開繼續(xù)運(yùn)動(dòng),點(diǎn)擊按鈕關(guān)閉窗口
ps:不建議使用這個(gè)特效,影響用戶體驗(yàn)
希望對你有所幫助!^_^!~~
相關(guān)文章
JavaScript中小數(shù)點(diǎn)精度丟失的原因以及解決方法
計(jì)算機(jī)再大的內(nèi)存它也存不下,所以不能存儲(chǔ)一個(gè)相對于數(shù)學(xué)來說的值,只能存儲(chǔ)一個(gè)近似值,所以當(dāng)計(jì)算機(jī)存儲(chǔ)后再取出來用時(shí)就會(huì)出現(xiàn)精度問題,下面這篇文章主要給大家介紹了關(guān)于JavaScript中小數(shù)點(diǎn)精度丟失的原因以及解決方法,需要的朋友可以參考下2023-10-10Javascript中判斷變量是數(shù)組還是對象(array還是object)
怎樣判斷一個(gè)JavaScript變量是array還是obiect,或許有很多初學(xué)者對此不是很清楚吧,下面為大家詳細(xì)解答下,希望對大家有所幫助2013-08-08js圖片上傳中file、bolb、base64圖片之間的相互轉(zhuǎn)化
這篇文章主要介紹了js圖片上傳中file、bolb、base64圖片之間的相互轉(zhuǎn)化,blob轉(zhuǎn)file,blob轉(zhuǎn)base64,base64轉(zhuǎn)file,使用canvas壓縮圖片,需要的朋友可以參考下2022-05-05js對象屬性名駝峰式轉(zhuǎn)下劃線的實(shí)例代碼
這篇文章主要介紹了js對象屬性名駝峰式轉(zhuǎn)下劃線的實(shí)例代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09JS無縫滾動(dòng)效果實(shí)現(xiàn)方法分析
這篇文章主要介紹了JS無縫滾動(dòng)效果實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了無縫滾動(dòng)的原理、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-12-12