如何实现youtube show&隐藏难忘的侧边栏?

时间:2017-04-23 17:35:15

标签: javascript jquery youtube

我想制作一个带有显示/隐藏功能的侧边栏,如Youtube侧边栏。我使用以下代码: -

   <div id="showmenu">Click Here</div>
     <div class="menu" style="display: none;">
   <ul>
   <li>Button1</li>
    <li>Button2</li>
   <li>Button3</li>
   </ul>
   </div>

&安培;的Javascript

$(document).ready(function() {
    $('#showmenu').click(function() {
            $('.menu').toggle("slide");
    });
});

我想像youtube一样显示/隐藏

希望你们能帮助我。

1 个答案:

答案 0 :(得分:0)

当用户隐藏侧边栏设置cookie时使用cookies,下次用户访问网站时检查是否设置了cookie,如果设置了cookie,则隐藏边栏。

将此课程添加到您的css

.toggled {
    display: none;
}

<div id="showmenu">Click Here</div>
<div class="menu" style="display: none;">
   <ul>
       <li>Button1</li>
       <li>Button2</li>
       <li>Button3</li>
   </ul>

的Javascript

    $(document).ready(function () {
    if (getCookie('sidebar')) {
        $('.menu').toggleClass("toggled");
    }
    $("#showmenu").click(function () {
        $('.menu').toggle("slide");
        if (getCookie('sidebar')) {
            deleteCookie('sidebar', 'hide');
        }else {
            setCookie('sidebar', 'hide');
        }
    });

    function setCookie(cname, cvalue) {
        var d = new Date();
        d.setTime(d.getTime() + (60 * 1000)); // set to expire in 60 seconds
        var expires = "expires=" + d.toUTCString();
        document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
    }

    function getCookie(cname) {
        var name = cname + "=";
        var decodedCookie = decodeURIComponent(document.cookie);
        var ca = decodedCookie.split(';');
        for(var i = 0; i <ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') {
                c = c.substring(1);
            }
            if (c.indexOf(name) == 0) {
                return c.substring(name.length, c.length);
            }
        }
        return "";
    }

    var deleteCookie = function(cname, cvalue) {
        document.cookie = cname + "=" + cvalue +  '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;path=/';
    };
});