Slide panel up after x seconds, close on click - SET COOKIE

北战南征 提交于 2019-12-12 18:22:27

问题


hoping someone can point me in the right direction.

We're trying to improve subscriptions to our mailing list. When a user visits our site after x amount of seconds, i'd like a panel to slide up asking them to sign up to the mailing list.

If they click any of the buttons (sign up, close, dont show again) I want to set a cookie so the panel doesn't show again.

I've mastered the slide up/down BUT i'm a newbie at the cookie side of things and not sure how to set it so when the cookie is set, the slide action doesn't occur again.

Here's my jQuery...

// MAILING LIST SLIDER //
        // set a delay of 3 seconds before mailing list panel appears 
        $("#mailingListPanelSlide").delay(3000).slideDown("slow");

        // set triggers to close the mailing list panel & set cookie
        $("a#closeButton, p.negativeActionFormButton").click(function(){
            $("div#mailingListPanelSlide").slideUp("slow");
            $.cookie("mailingListPanel", "dontshow");
            }); 

        // HELP!!!! if cookie is set to collapsed, then don't perform slide down/hide panel altogether?
        var mailingListPanel = $.cookie("mailingListPanel");
        if (mailingListPanel == "dontshow") {
            $("div#mailingListPanelSlide").css("display","none");
            };


        //END MAILING LIST SLIDER //    

回答1:


You must have the jQuery cookie plugin if you want to use $.cookie https://github.com/carhartl/jquery-cookie

if($.cookie("mailingListPanel") !== 'dontshow'){
    // set a delay of 3 seconds before mailing list panel appears 
    $("#mailingListPanelSlide").delay(3000).slideDown("slow");
}else{
    $("div#mailingListPanelSlide").css("display","none");
}
// set triggers to close the mailing list panel & set cookie
$("a#closeButton, p.negativeActionFormButton").click(function(){
    $("div#mailingListPanelSlide").slideUp("slow");
    $.cookie("mailingListPanel", "dontshow");
}); 



回答2:


   if ($.cookie("mailingListClosed") == null && $.cookie("mailingListNeverShow") == null) {
            $("#mailingListPanelSlide").delay(4000).slideDown("slow");
        };

        // Trigger for close panel button. Close panel and set session cookie
        $("a#closeButton").click(function () {
            $("div#mailingListPanelSlide").slideUp("slow");
            $.cookie("mailingListClosed", "true", { path: "/" });
        });

        // Trigger for never show panel button. Close panel and set persistent cookie
        $("p.negativeActionFormButton").click(function () {
            $("div#mailingListPanelSlide").slideUp("slow");
            $.cookie("mailingListNeverShow", "true", { expires: 365, path: "/" });
        });

        // Set persistent cookie if user clicks on the newsletter button as well
        $("p.genericLinkButton").click(function () {
            $.cookie("mailingListNeverShow", "true", { expires: 365, path: "/" });
        });


来源:https://stackoverflow.com/questions/8171024/slide-panel-up-after-x-seconds-close-on-click-set-cookie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!