简体   繁体   English

刷新后如何使元素不可见

[英]How can I make an element not visible after refresh

Basically I put a lightbox with a video in my website which shows at the beginning, you can skip it using a button or pressing "Esc" key, but what i want is it only shows once, I mean if I do a refresh on the site, or if I go back the the Home page, I want the div of the video no longer displays. 基本上,我在网站上放了一个带有视频的灯箱,该灯箱在开始时会显示,您可以使用按钮或按“ Esc”键跳过它,但是我想要的是它只显示一次,这意味着我要刷新网站,或者如果我返回首页,则我希望视频的div不再显示。

Here's what i tried to do 这是我尝试做的

 <article id="video_holder">
    <iframe src="https://player.vimeo.com/video/123859666?autoplay=1&color=3551a4&title=0&byline=0&portrait=0&controls=0" width="100%" height="100%" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
    <a class="close_video"> <i class="fa fa-angle-double-right"></i> Saltar Video </a>
</article>

 <script>
 $('.close_video').click(function(){
     $("#video_holder").fadeOut(500, function() {
         // Here I tried to remove the window forever
         $(this).remove();
     });          
 });
 </script>

Anyone knows how can I achieve it? 有人知道我该怎么做吗?

As other have suggested, the correct way to do this is with a cookie. 正如其他人所建议的那样,正确的方法是使用cookie。 Use the following functions to set and read the cookie: 使用以下功能来设置和读取cookie:

function read_video_cookie(){
        var name = "skip_video=";
        var all_cookies = document.cookie.split(';');

        for(var i = 0; i < all_cookies.length; i++){
            var c = all_cookies[i];
            while(c.charAt(0) == ' ')
                c = c.substring(1);
            if (c.indexOf(name) == 0)
                return JSON.parse(c.substring(name.length, c.length));
        }

        return false;
    }

    function set_video_cookie(){
        $skip_video = true;
        var d = new Date();
        d.setTime(d.getTime() + (365*24*60*60*1000)); // Cookie expires in 1 year
        var expires = "expires=" + d.toUTCString();
        document.cookie = "skip_video=" + "true" + "; " + expires + ";";
        $('.close_video').click(function(){
            $("#video_holder").fadeOut(500, function() {
                $(this).remove();
            });          
        });
    }

Then, in your document ready function, check the value of the cookie and insert the video element if needed: 然后,在准备文档的功能中,检查cookie的值,并在需要时插入video元素:

var $skip_video;

        $(function(){
            $skip_video = read_video_cookie();

            if(!$skip_video){
                $("#video_holder").append('<iframe src="https://player.vimeo.com/video/123859666?autoplay=1&color=3551a4&title=0&byline=0&portrait=0&controls=0" width="100%" height="100%" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe><a class="close_video"> <i class="fa fa-angle-double-right"></i> Saltar Video </a>');
            } else {

                $("#video_holder").fadeOut(500, function() {
                    $(this).remove();
                }); 

            }      
            set_video_cookie();     
        });

Updated: 更新:

This method should remove the delay you're dealing with. 此方法应消除您正在处理的延迟。 You should also change your markup to: 您还应该将标记更改为:

 <article id="video_holder"></article>

Since you're using jquery already, there's a plugin which makes getting and setting cookies much easier: 由于您已经在使用jQuery,因此有一个插件可以使获取和设置Cookie更加容易:

https://github.com/carhartl/jquery-cookie https://github.com/carhartl/jquery-cookie

To set the cookie: 设置cookie:

$.cookie('myCookie', 'value');

To then, get the cookie: 为此,获取cookie:

$.cookie('myCookie');

Then, on page load, you can show your video if it's set; 然后,在页面加载时,您可以显示视频(如果已设置); this would require you to keep the video hidden by default. 默认情况下,这需要您将视频隐藏起来。 Using an inline style (if you want): style="display:none" : 使用内联样式(如果需要): style="display:none"

Then, on document ready, you should show it if the cookie's not set: 然后,在准备好文档后,如果未设置cookie,则应显示它:

$( document ).ready(function() {
    if( !$.cookie('myCookie') ){
        $("#video_holder").show();
        $.cookie('myCookie', 'watched');
    }
}

Also, you'll probably want to set autoPlay in your vimeo URL to false. 另外,您可能需要将vimeo URL中的autoPlay设置为false。 I'm not sure how it behaves if the container isn't visible. 我不确定容器不可见时的行为。

I'd suggest sessionStorage which acts like a document store on the user's browser. 我建议使用sessionStorage ,其作用类似于用户浏览器上的文档存储。 So you'd do: 所以你会做:

 $('.close_video').click(function(){
     $("#video_holder").fadeOut(500, function() {
         sessionStorage.setItem('viewed_my_video', true);
         $(this).remove();
     });          
 });

I would also encourage you to use javascript to render the video in as oppose to removing it from the dom. 我也鼓励您使用javascript渲染视频,以反对将其从dom中删除。 which will allow you to do something like: 这将使您可以执行以下操作:

if(!sessionStorage.getItem('viewed_my_video')) {
  $('#video-holder').append('<iframe src="https://player.vimeo.com/video/123859666?autoplay=1&color=3551a4&title=0&byline=0&portrait=0&controls=0" width="100%" height="100%" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>');
}

There are betters ways of rendering html via javascript but figure that is outside the scope of this question. 有更好的方式通过javascript呈现html,但是该图不在此问题的范围内。 ( But if you're curious checkout underscore / lodash / handlebars ). (但是,如果您对结帐下划线/ lodash / handlebars感到好奇)。

As others suggested cookies are another way to go about this. 正如其他人建议的那样, cookies是解决此问题的另一种方法。 I favor sessionStorage over cookies because it's easier to use vs parsing out a long string. 与cookie相比,我更喜欢sessionStorage ,因为它比解析长字符串更容易使用。 Here's a great StackOverflow answer on the differences: What is the difference between localStorage, sessionStorage, session and cookies? 这是关于区别的一个很好的StackOverflow答案: localStorage,sessionStorage,session和cookie之间有什么区别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM