简体   繁体   English

延迟Turbolink的访问以关闭画布菜单以关闭

[英]Delay Turbolink's visit for off-canvas menu to close

I'm using TurboLinks and a off-canvas menu that closes with an animation. 我正在使用TurboLinks和一个关闭了动画的画布菜单 By the time, Turbolinks changes the page before the menu is completely closed and it seems a little rushed. 到那时,Turbolinks会在菜单完全关闭之前更改页面,这似乎有些仓促。 I've tried adding a timeouts and tinkering with TurboLinks' events but without any luck. 我尝试添加超时并修改TurboLinks的事件,但是没有任何运气。 Javascript/Jquery is really not my strong suit, so, my question: is there a way to delay the firing of the TurboLinks' request so the menu can close normally? Javascript / Jquery 确实不是我的强项,所以,我的问题是:有没有办法延迟TurboLinks请求的触发,以便菜单可以正常关闭?

Pd. I'm developing with Laravel. 我正在与Laravel合作。

Update: 更新:

In the documentation I've found that you can't cancel de visit before it starts. 文档中,我发现您无法在开始之前取消访问。 With that in mind, I set this 考虑到这一点,我将其设置为

$(document).on('turbolinks:before-visit', function(e){
    var url = event.data.url;
    console.log(url);
    event.preventDefault();
    e.stopImmediatePropagation();

    setTimeout(function(url){
        Turbolinks.visit(url);
    }, 1000);
});

But, as I said in the comments line with @OhGodWhy everything crumbles, the page gets stuck and the error "maximum call stack size exceeded" is shown multiple times in the console. 但是,正如我在@OhGod的注释行中所说的那样, 为什么一切都崩溃了,所以该页面被卡住了,并且在控制台中多次显示“超出最大调用堆栈大小”错误。

There is an RFC out there that dictates you cannot slow/stop the process of a user leaving the page without their intervention; 那里有一个RFC,指示您不能在没有用户干预的情况下减慢/停止用户离开页面的过程。 hence why you see alert windows popping up whenever you want to leave the page, but you can click 'continue' to proceed to leave anyway. 因此,为什么每次要离开页面时都会看到alert窗口弹出,但是您仍然可以单击“继续”以继续离开。

That said, I can give you a solution. 也就是说,我可以给您解决方案。 All of their animations are CSS3 driven, the only javascript you need to worry about is the one that toggles the class. 他们所有的动画都是CSS3驱动的,您唯一需要担心的JavaScript是切换类的JavaScript。

$('.sidebar').on('click', 'a', function(e){
    e.preventDefault();

    var $this = $(this);

    if ($('body').hasClass('st-menu-open')) {
        $('body').removeClass('st-menu-open')
        setTimeout(function(){
           Turbolinks.visit($this.prop('href'));
        }, 500);
    }
});

All we're doing here is caputring the click in your nav, if the body has the class, then we're going to remove it. 我们在这里所做的就是在您的导航中添加点击,如果主体具有该类,那么我们将其删除。 However, we're going to wait 500ms which corresponds to the CSS transition delay that the library uses, until we execute the redirection. 但是,我们将等待500ms ,这与库使用的CSS transition delay相对应,直到执行重定向。 This way, after 500 ms, the transition will be completed and you can redirect the user. 这样,在500毫秒后,过渡将完成,您可以重定向用户。

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

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