一聚教程网:一个值得你收藏的教程网站

热门教程

怎样用javascript实现带关闭窗口的浮动广告

时间:2023-07-17 10:03:16 编辑:袖梨 来源:一聚教程网

首先浮动的窗口就是一个层,在这个层上放置一个小的层,代表关闭按钮,可以使用背景图片,或者img,用小层装,是为来方便固定位置,在这个小层中可以写js代码,当鼠标点击是获得大层,修改大层的隐藏属性,这样就实现来,整个层的关闭

63a02a2b0a642.jpg

js 代码,随页面滚动而滚动的浮动广告效果(带关闭按钮)

随滚动而滚动,css就可以实现,也就是固定在屏幕固定位置,用 position:fixed;即可,关闭按钮可以用document.getElementById('').style.display='none';即可,如

div style="width:500px; height:200px; background-color:#F00;position:fixed;left:100px;top:200px;" id="test"

button onclick="document.getElementById('test').style.display='none';"关闭/button

/div

其中left和top都是相对于屏幕的位置

可关闭两边飘浮对联广告代码jquery特效宽屏显示

html

head

meta http-equiv="Content-Type" content="text/html; charset=gb2312" /

style type="text/css"

#ad1,#ad2{

    -webkit-transition: all .5s ease-out;

    -moz-transition: all .5s ease-out;

    -o-transition: all .5s ease-out;

    transition: all .5s ease-out;

    POSITION:absolute;

    TOP:120px;

    width:100px;

    height:200px;

    border:1px solid red;

}

#close1,#close2{

font-size:10px;

background:gray;

float:right;

}

#close1:hover,#close2:hover{cursor:pointer}

/style

/head

body

div id="mm" style="height:2000px;background:#f5f5f5;width:500px"

/div

DIV id="ad1" style='left:2px;'

div id="close1"关闭/div

/div

DIV id="ad2" style='right:2px;'

div id="close2"关闭/div

/div

script type="text/javascript" src="js/jquery.js" /script

script language="JavaScript" type="text/javascript"

$("#close1").click(function(){ //关闭广告

$(this).parent().remove();

})

$("#close2").click(function(){

$(this).parent().remove();

})

document.onscroll=function(){ //滑动保持广告位置

$("#ad1").style.top=document.body.scrollTop+120+'px';

$("#ad2").style.top=document.body.scrollTop+120+'px';

};

/script

/body

/html

带关闭的漂浮广告代码

JS代码实现对联式浮动广告演示!

self.onError=null;

currentX = currentY = 0;

whichIt = null;

lastScrollX = 0; lastScrollY = 0;

NS = (document.layers) ? 1 : 0;

IE = (document.all) ? 1: 0;

function heartBeat() {

if(IE) { diffY = document.body.scrollTop; diffX = document.body.scrollLeft; }

if(NS) { diffY = self.pageYOffset; diffX = self.pageXOffset; }

if(diffY != lastScrollY) {

percent = .1 * (diffY - lastScrollY);

if(percent 0) percent = Math.ceil(percent);

else percent = Math.floor(percent);

if(IE) document.all.floater.style.pixelTop += percent;

if(NS) document.floater.top += percent;

lastScrollY = lastScrollY + percent;

}

if(diffX != lastScrollX) {

percent = .1 * (diffX - lastScrollX);

if(percent 0) percent = Math.ceil(percent);

else percent = Math.floor(percent);

if(IE) document.all.floater.style.pixelLeft += percent;

if(NS) document.floater.left += percent;

lastScrollX = lastScrollX + percent;

}

}

function checkFocus(x,y) {

stalkerx = document.floater.pageX;

stalkery = document.floater.pageY;

stalkerwidth = document.floater.clip.width;

stalkerheight = document.floater.clip.height;

if( (x stalkerx x (stalkerx+stalkerwidth)) (y stalkery y (stalkery+stalkerheight))) return true;

else return false;

}

function grabIt(e) {

if(IE) {

whichIt = event.srcElement;

while (whichIt.id.indexOf("floater") == -1) {

whichIt = whichIt.parentElement;

if (whichIt == null) { return true; }

}

whichIt.style.pixelLeft = whichIt.offsetLeft;

whichIt.style.pixelTop = whichIt.offsetTop;

currentX = (event.clientX + document.body.scrollLeft);

currentY = (event.clientY + document.body.scrollTop);

} else {

window.captureEvents(Event.MOUSEMOVE);

if(checkFocus (e.pageX,e.pageY)) {

whichIt = document.floater;

StalkerTouchedX = e.pageX-document.floater.pageX;

StalkerTouchedY = e.pageY-document.floater.pageY;

}

}

return true;

}

function moveIt(e) {

if (whichIt == null) { return false; }

if(IE) {

newX = (event.clientX + document.body.scrollLeft);

newY = (event.clientY + document.body.scrollTop);

distanceX = (newX - currentX); distanceY = (newY - currentY);

currentX = newX; currentY = newY;

whichIt.style.pixelLeft += distanceX;

whichIt.style.pixelTop += distanceY;

if(whichIt.style.pixelTop document.body.scrollTop) whichIt.style.pixelTop = document.body.scrollTop;

if(whichIt.style.pixelLeft document.body.scrollLeft) whichIt.style.pixelLeft = document.body.scrollLeft;

if(whichIt.style.pixelLeft document.body.offsetWidth - document.body.scrollLeft - whichIt.style.pixelWidth - 20) whichIt.style.pixelLeft = document.body.offsetWidth - whichIt.style.pixelWidth - 20;

if(whichIt.style.pixelTop document.body.offsetHeight + document.body.scrollTop - whichIt.style.pixelHeight - 5) whichIt.style.pixelTop = document.body.offsetHeight + document.body.scrollTop - whichIt.style.pixelHeight - 5;

event.returnValue = false;

} else {

whichIt.moveTo(e.pageX-StalkerTouchedX,e.pageY-StalkerTouchedY);

if(whichIt.left 0+self.pageXOffset) whichIt.left = 0+self.pageXOffset;

if(whichIt.top 0+self.pageYOffset) whichIt.top = 0+self.pageYOffset;

if( (whichIt.left + whichIt.clip.width) = (window.innerWidth+self.pageXOffset-17)) whichIt.left = ((window.innerWidth+self.pageXOffset)-whichIt.clip.width)-17;

if( (whichIt.top + whichIt.clip.height) = (window.innerHeight+self.pageYOffset-17)) whichIt.top = ((window.innerHeight+self.pageYOffset)-whichIt.clip.height)-17;

return false;

}

return false;

}

function dropIt() {

whichIt = null;

if(NS) window.releaseEvents (Event.MOUSEMOVE);

return true;

}

!-- DRAG Drop CODE --

if(NS) {

window.captureEvents(Event.MOUSEUP|Event.MOUSEDOWN);

window.onmousedown = grabIt;

window.onmousemove = moveIt;

window.onmouseup = dropIt;

}

if(NS || IE) action = window.setInterval("heartBeat()",1);

将以上内容保存到一个文件:code.js,将以下内容放到广告的页面,修改test.jpg,改成自己的图片或者FLASH文件

SCRIPT language="javascript" src="code.js"/SCRIPT

div id="floater" style="left:11px;top: 98px;width:100%;position: absolute;visibility:visible;z-index:1"

div id="dllayer3" style="left:0px; position: absolute; visibility:visible; z-index:2; width: 101px; height: 286px;"img src="test.jpg" width="80" height="100" border="0"

/div

div id=dllayer2 style='right:0px; position: absolute; visibility:visible; z-index:2; left: 875px; top: 0px; width: 80px; height: 100px;'img src="test.jpg" width="80" height="100" border="0"/div

/div

热门栏目