jquery对联广告特效代码:可随滚动条滚动的对联广告

发布时间:2021-01-12编辑:脚本学堂
如何用jquery实现对联广告效果,这里分享一例对联广告代码,可随滚动条滚动的对联广告,带有关闭广告的按钮,需要的朋友参考下。

对联广告,如图:

<a href=http://www.jb200.com/jb/jquery/ target=_blank class=infotextkey>jquery</a>对联广告代码

用到的图片,如下:
对联广告原图1

对联广告原图2

jquery对联广告代码
 

复制代码 代码示例:
<script type="text/javascript">
$(document).ready(function(){
$(".fixediv a").click(function(){
$(".fixediv").fadeOut(400);
});
$(".fixediv").floatadv();
});
jQuery.fn.floatadv = function(loaded) {
var obj = this;
body_height = parseInt($(window).height());
block_height = parseInt(obj.height());
top_position = parseInt((body_height/2) - (block_height/2) + $(window).scrollTop());
if (body_height<block_height) { top_position = 0 + $(window).scrollTop(); };
if(!loaded) {
obj.css({'position': 'absolute'});
obj.css({ 'top': top_position });
$(window).bind('resize', function() {
obj.floatadv(!loaded);
});
$(window).bind('scroll', function() {
obj.floatadv(!loaded);
});
} else {
obj.stop();
obj.css({'position': 'absolute'});
obj.animate({ 'top': top_position }, 400, 'linear');
}
}
</script>

对联广告完整代码
 

复制代码 代码示例:

<!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>jquery对联广告悬浮特效跟随滚动条滚动</title>
</head>
<body>
<style type="text/css">
*{margin:0;padding:0;list-style-type:none;}
.fixediv{position:fixed;top:140px;z-index:9999;width:140px;height:216px;background:#ddd;}
.fixediv img{float:left;}
.fixediv a.close{display:block;height:30px;line-height:30px;background:#fff;font-size:14px;padding:0 10px;color:#5e5e5e;text-decoration:none;text-align:center;}
.leftadv{left:0px;}
.rightadv{right:0px;}
</style>
<div class="fixediv leftadv">
<a href="http://www.jb200.com/"><img src="images/180.jpg" width="140" height="186" alt="脚本学堂" /></a>
<a class="close" href="javascript:void(0);">关闭广告</a>
</div>
<div class="fixediv rightadv">
<a href="http://www.jb200.com/"><img src="images/280.jpg" width="140" height="186" alt="脚本学堂" /></a>
<a class="close" href="javascript:void(0);">关闭广告</a>
</div>
<div style="height:1200px;"></div>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".fixediv a").click(function(){
$(".fixediv").fadeOut(400);
});
$(".fixediv").floatadv();
});

jQuery.fn.floatadv = function(loaded) {
var obj = this;
body_height = parseInt($(window).height());
block_height = parseInt(obj.height());

top_position = parseInt((body_height/2) - (block_height/2) + $(window).scrollTop());
if (body_height<block_height) { top_position = 0 + $(window).scrollTop(); };

if(!loaded) {
obj.css({'position': 'absolute'});
obj.css({ 'top': top_position });
$(window).bind('resize', function() {
obj.floatadv(!loaded);
});
$(window).bind('scroll', function() {
obj.floatadv(!loaded);
});
} else {
obj.stop();
obj.css({'position': 'absolute'});
obj.animate({ 'top': top_position }, 400, 'linear');
}
}
</script>
</body>
</html>