Append div to body when URL hash # changes

白昼怎懂夜的黑 提交于 2019-12-24 15:33:26

问题


I'm using curtain.js and would like to keep a DIV (which holds navigation) visible at all times except for when the user is looking at the very first panel, i.e. at the top of the page.

Currently the DIV resides within panel two

I'm thinking perhaps of using the hash change as you scroll through the page to trigger an append to the body. Curtain.js creates an individual URL for each panel and the URL changes each time a panel is brought into view.

I can append the div to the body (below) but I need to work out when to do this but I am unsure how? Could anyone help me out?

$("body").append($('.nav-wrap'));

回答1:


you can use onhashchange event:

The hashchange event fires when a window's hash changes

$(window).bind('hashchange', function() {
   $("body").append($('.nav-wrap'));
})



回答2:


You can use JQuery to bind the Event

$(window).bind('hashchange', function(){ ... });

And add some workarounds for when it doesn't have the onhashchange event. jQuery - hashchange event




回答3:


Ok well instead of using some hacky solution, after much digging around in the plugin's file, I just added:

$("body").append($('.nav-wrap'));

to the setHash function on line 491. Works a treat.



来源:https://stackoverflow.com/questions/11454820/append-div-to-body-when-url-hash-changes

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