简体   繁体   English

单击 rails link_to 后未触发 jQuery 就绪

[英]jQuery ready not fired after rails link_to is clicked

im using the javascript below to make an image "fade in" triggered by a scroll event.我使用下面的 javascript 使图像“淡入”由滚动事件触发。 But the .hide im using to initially hide the image only works when the page loads initially, typing localhost:3000.但是用于最初隐藏图像的 .hide 仅在页面最初加载时才有效,键入 localhost:3000。

Once i'm in the site if i try to click a link to go back to the main page,进入网站后,如果我尝试单击链接返回主页,

<%= link_to 'Main', root_path %>

the main page loads with out the .hide working主页加载没有 .hide 工作

Why does the .hide work when the page initially loads but if i click a link to the same page the .hide does not work.为什么 .hide 在页面最初加载时起作用,但如果我单击指向同一页面的链接,则 .hide 不起作用。 the rest of the Javascript works no matter what, i can scroll and the image fades in and out depending on the scroll position, but i need it to start hidden, and that only works when the page loads initially help Javascript 的其余部分无论如何都可以工作,我可以滚动并且图像根据滚动位置淡入淡出,但我需要它开始隐藏,并且只有在页面加载最初时才有效帮助

function isElementVisible(elementToBeChecked) {
    var TopView = $(window).scrollTop();
    var BotView = TopView + $(window).height();
    var TopElement = $(elementToBeChecked).offset().top + 100;
    var BotElement = TopElement + $(elementToBeChecked).height();
    return ((BotElement <= BotView) && (TopElement >= TopView));
}

$(document).ready(function(){
    $("#ghost_logo").hide();// hide it initially
});    

$(window).scroll(function(){
    isOnView = isElementVisible("#logoDiv");

    if (isOnView) {
        //fade out small image once main logo is in view
        $('#ghost_logo').fadeOut();
    } else {
        //fade in small image once main logo is out of view
        $('#ghost_logo').fadeIn();
    }        
});

You are having a tipical problem with turbolinks, turbolinks make that DOM doesn't change when you navigate through a links, for this reason, the ready event from jQuery is never fired, you can solve this in three ways.您遇到了 turbolinks 的典型问题,turbolinks 使 DOM 在您浏览链接时不会改变,因此,jQuery 的 ready 事件永远不会被触发,您可以通过三种方式解决这个问题。

You can try something like:您可以尝试以下操作:

document.addEventListener('page:load', function(){
    $("#ghost_logo").hide();
});

or if this doesn't work either, try navigate with out turbolinks change a parameter of link:或者,如果这也不起作用,请尝试在不使用 turbolinks 的情况下进行导航,更改链接的参数:

 <%= link_to 'Main', root_path ,{ :'data-no-turbolink' =>  "true" } %>

or if you want you can use this jQuery-turbolinks gem is a gem provided for solving this kind of issues.或者,如果您愿意,您可以使用此jQuery-turbolinks gem是为解决此类问题而提供的 gem。

For more information refer to turbolinks repository on github有关更多信息,请参阅github上的turbolinks 存储库

document.addEventListener('page:load', function(){
    $("#ghost_logo").hide();
});

Is not always working.并不总是有效。 Better use:更好的使用:

document.addEventListener('turbolinks:load', function(){
    $("#ghost_logo").hide();
});

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

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