CSS+JS+Cookie如何实现页脚固定广告展示(支持关闭、一段时间内不显示)

秋天来啦,秋天来啦;天空像一块覆盖在大地上的蓝宝石,它已经给秋风抹试得洁净而明亮。棉花笑嘻嘻的来了,它听说好朋友—秋姑娘来了,笑的咧开了嘴。玉米在房顶上唱着歌跳着舞,别提多开心了。路边两只黑色的小狗在玩耍,看它那可爱的样子,让人忍不住的笑。石榴也许太兴奋了,竟然笑破了肚皮,苹果跑着来了,当然脸蛋是红彤彤的。

经常看到有些网站为了吸引用户注意,在页面底部放置固定横幅广告,用户滚动页面时,横幅广告一直固定在页底,并不随页面滚动而滚动,当然一般允许用户可以关闭广告条,并设置一定时间内不再显示此广告条。本文将介绍使用CSS+Cookie来实现这一效果。

源码下载:点此下载

HTML

首先,我们将横幅广告的html代码放到页面最底部,因为是最后加载的。也可以使用外部js动态插入到页面底部。整个HTML结构由遮罩层.float_layer,内容层.float_content组成,其中.float_bg为广告部分,内容可以是图片、文字等任意形式的html元素,.float_close是关闭按钮,用户不喜欢广告可以关闭展示。

复制代码
代码如下:

<div class="float_mask" id="float_mask">
<div class="float_layer">
</div>
<div class="float_content clearfix">
<div class="float_bg">
<a target="_blank" rel="nofollow noopener noreferrer" href="<a rel="nofollow noopener noreferrer" href="https://www.haodaima.com/">https://www.haodaima.com/</a>" title="广告部分">
<div class="float_slogan"><!--广告内容--></div>
</a>
</div>
<div class="float_close">
<a onclick="closeFootAd()" rel="nofollow noopener noreferrer" href="#" title="我知道了"></a>
</div>
</div>
</div>

CSS

我们使用CSS将广告条固定在页脚,以及展示半透明遮罩效果、广告关闭按钮等效果。我们知道position: fixed是固定元素位置,配合bottom、right等属性可以将元素固定在页面某个位置,并不随页面滚动而滚动。使用opacity属性可以实现透明效果。我们给.float_slogan一个background属性,将广告图片作为背景加入,当然你也可以不需要这样做,直接在上面的html中加入图片或文字。

复制代码
代码如下:

.float_mask{position: fixed;z-index: 19999;display:none;width: 100%;right: 0; bottom: 0;height: 105px;_bottom: auto;_width: 100%;_position: absolute;
_top: expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0)));}
.float_layer{position: absolute;left: 0;top: 0;z-index: 1;width: 100%;height: 100%;background: #071828;filter: alpha(opacity=80);opacity: 0.80;}
.float_content{ position: relative;z-index: 2;width: 1005px;height: 100%;margin: 0 auto;padding-left: 70px;}
.float_bg, .float_close{ float: left;}
.float_bg{position: relative;width: 820px;height: 135px;margin-top: -27px;}
.float_slogan {position: absolute; background: url("footer_ad.png") 0 0 no-repeat;}
.float_slogan{left: 0;bottom: 0;width: 800px;height: 135px;cursor: pointer;}
.float_close{width: 60px;margin-top: 30px;}
.float_close a {display: block;width: 53px; height: 52px; margin-left: 7px; background: url("close.png") 0 0 no-repeat;-webkit-transition: all 400ms;}

JAVASCRIPT

我们初次打开页面时,Javascript先去检测页底横幅广告关联的cookie信息,如果cookie表示的信息是关闭的,则不显示页底广告,反之显示页底广告。我们在点击关闭按钮的时候会调用closeFootAd()函数,点击关闭按钮,则将广告条隐藏,即关闭,并设置cookie相关值。以下是整个javascript的操作代码:

复制代码
代码如下:

window.onload = function(){
if(getCookie("footad")==0){
document.getElementById("float_mask").style.display="none";
}else{
document.getElementById("float_mask").style.display="block";
}
}
//关闭底部广告
function closeFootAd() {
document.getElementById("float_mask").style.display="none";
setCookie("footad","0");
}

//设置cookie
function setCookie(name,value){
var exp = new Date();
exp.setTime(exp.getTime() + 1*60*60*1000);//有效期1小时
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
//取cookies函数
function getCookie(name){
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;
}

完!

以上就是CSS+JS+Cookie如何实现页脚固定广告展示(支持关闭、一段时间内不显示)。我只愿蓬勃生活在此时此刻,无所谓去哪,无所谓见谁,那些我将要去的地方,都是我从未谋面的故乡;那些我将要见的人,都会成为我的朋友。——萧红更多关于CSS+JS+Cookie如何实现页脚固定广告展示(支持关闭、一段时间内不显示)请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
css让页脚保持在底部位置的四种方案

CSS如何使用Flex和Grid布局如何实现3D骰子

Flex布局史上最简单使用语法教程

新的CSS 伪类函数 :is() 和 :where()示例详解

纯CSS打字动画的如何实现示例