仿BBC cookie message提示功能

功能描述:当用户访问网站时,浏览器会判断是否缓存cookie。若已有缓存,不会提示cookie message;否则,显示cookie message。当用户点击continue按钮时,表示用户同意浏览器缓存cookie,将cookie信息存储在cookie文件,在cookie有效期内不会再提示cookie message;当用户点击change 或者find链接时,跳转到cookie policy 页面。

Html 代码:.csm-message是提示信息,.navbanner是导航部分。

<div class="csm-cookies">
    <div class="cookies-banner row">
        <div class="col-md-3">
            <h2 class="cookies-title">Cookies on the Cashmirino website</h2>
        </div>
        <div class="col-md-6">
            <p class="cookies-content">
                This website uses cookies. Cookies are stored in your computer by your browser in the form of small text parcels sent to and from a website. Cookies help a website to identify different users which helps us to provide you with a good experience when you browse our website and also allows us to improve our website. <a href="@Url.Action("CookiePolicy", "Static")">This policy</a> explains how this site uses cookies and how you can control them.
            </p>
        </div>
        <div class="col-md-3">
            <ul class="cookies-options">
                <li id="cookies-continue">
                    <span class="glyphicon glyphicon-ok"></span><a class="continue"> Continue</a>
                </li>
                <li id="cookies-settings">
                    <a href="@Url.Action("CookiePolicy", "Static")"><span class="glyphicon glyphicon-cog"></span> Change settings</a>
                </li>
                <li id="cookies-more">
                    <a href="@Url.Action("CookiePolicy", "Static")"><span class="glyphicon glyphicon-question-sign"></span> Find out more</a>
                </li>
            </ul>
        </div>
    </div>
</div>
<div class="navbanner"></div>

CSS代码:

.csm-cookies { background: $color-brown; color: #fff; padding: 16px 0; display: none; line-height: 1; font-size: 1.3em;
    a { color: #fff;cursor: pointer; }
    a:active, a:focus, a:hover, button:active, button:focus, button:hover { color: #e8dbd1; border: none; text-decoration:underline;}
    span { font-size: 0.8em; }
    .cookies-banner {
        margin: 0 auto;
        .cookies-title {
            font-size: 1.3em;
            font-family: "Carrotflower",sans-serif;
            line-height: 1.2em;
            margin-top: 40px;
            font-weight: 700;
            @media screen and (min-width: 600px) { width: 100%; margin-right: 3.5%; float: left; }
        }
        .continue{font-size:1.3em;text-decoration:none !important;}
        .cookies-content { margin: 0; padding: 0; border: 0; font: inherit; vertical-align: baseline; width: 100%; font-family: "Moderat",sans-serif; line-height: 1.11em; color:#ddd}
        .cookies-options { list-style: none; margin-top: 25px;
            /*button { background-color: transparent; background-repeat: no-repeat; text-decoration: underline; line-height: 1.231em; padding: 0; cursor: pointer; border: none; }*/

            li { padding-top: 8px; }
            li:first-child { padding-top: 0; }
        }
        @media screen and (min-width:1008px) { max-width: 976px; padding: 0 16px; }
        @media screen and (min-width:400px) { padding: 0 16px; }
    }
    .modal-close { right: 5px; top: 6px; }
    p.title { text-align: center; font-size: 14px; padding: 0; margin: 0; }
}
.navbanner { position: relative; }

JavaScript代码:

function checkCookie() {
    if ($.cookie('cookies-on-website') !== 'continued') {
        $('.csm-cookies').slideDown();
    }
}

checkCookie();

$('.csm-cookies .continue').click(function () {
    $.cookie('cookies-on-website', 'continued', { expires: 365 });
    $('.csm-cookies').slideUp();
    //location.reload(true);
});

知识点回顾:

1. CSS position注意事项

    参考学习:https://segmentfault.com/a/1190000006924181

    我的项目中,.navbanner包含多个不同功能的子导航,部分导航使用position:fixed固定在页面顶部,根据需求,cookie message要显示在导航上方。position绝对定位是相对于其父级元素的,所以我为.navbanner添加position:relative达到目的。

2.hide()与slideUp()的区别

    hide()直接隐藏目标元素,与show()显示目标元素对应使用。

    slideUp()向上滑动隐藏目标元素,与slideDown()向下滑动显示目标元素对应使用。

3.插件jquery.cookie.js的使用

    参考学习:https://blog.csdn.net/kevinwuwenboa/article/details/53786857

  (官方手册) https://github.com/carhartl/jquery-cookie#readme

基本语法:

  1)读取cookie           

       $.cookie(cookieName)  cookieName:要读取cookie的名称 

       e.g. $.cookie("username")

  2)写入设置cookie值  

       $.cookie(cookiName,cookieValue,[option]) 

       [option] 参数:

           expires:有效日期。若不设置,则默认有效期为浏览器关闭前。

           path:cookie保存路径。默认值为创建页面路径。

           domin:cookie域名属性。默认值为创建页面域名。若主域名二级域名有效则要设置“.xxx.com”

           secure:一个布尔值,表示传输cookie值时,是否需要一个安全协议         

       e.g. $.cookie("username","Yuki",{expires:3000;path:'D:/'}); 

              $.cookie("username",Null)//销毁名称为username的cookie

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值