jQuery漂浮广告插件是一种基于jQuery库的JavaScript插件,用于在网页上创建浮动广告。这些广告通常会在页面上浮动,跟随用户的滚动行为,以达到吸引用户注意力的效果。
以下是一个简单的jQuery漂浮广告插件示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Floating Ad</title>
<style>
#floating-ad {
position: absolute;
top: 10px;
right: 10px;
background-color: #f9f9f9;
padding: 10px;
border: 1px solid #ccc;
z-index: 1000;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div id="floating-ad">
<p>这是一个漂浮广告!</p>
<button onclick="closeAd()">关闭</button>
</div>
<script>
$(document).ready(function() {
var ad = $('#floating-ad');
var adWidth = ad.outerWidth();
var adHeight = ad.outerHeight();
var windowWidth = $(window).width();
var windowHeight = $(window).height();
ad.css({
'left': (windowWidth - adWidth) / 2,
'top': windowHeight - adHeight - 10
});
$(window).scroll(function() {
var scrollTop = $(this).scrollTop();
ad.css('top', scrollTop + windowHeight - adHeight - 10);
});
$(window).resize(function() {
var newWindowWidth = $(this).width();
var newWindowHeight = $(this).height();
ad.css({
'left': (newWindowWidth - adWidth) / 2,
'top': newWindowHeight - adHeight - 10
});
});
});
function closeAd() {
$('#floating-ad').remove();
}
</script>
</body>
</html>
$(document).ready()
中正确绑定滚动事件,并更新广告位置。$(document).ready()
中正确计算广告的初始位置,并在窗口大小变化时重新计算。通过以上方法,可以有效解决jQuery漂浮广告插件在使用过程中遇到的常见问题。
没有搜到相关的文章