简体   繁体   English

刷新JavaScript而不重新加载页面

[英]Refresh javascript without reloading the page

I am facing a problem where i have used custom scroll from JavaScript which will calculate the block's width and height from JS. 我遇到的一个问题是我使用了JavaScript的自定义滚动,它将通过JS计算块的宽度和高度。 the problem is, when the viewport is reduced, the particular block is hidden so from there if the window size is increased, the block with custom scroll won't show up because it wasn't able to calculate current width and height. 问题是,当缩小视口时,特定的块将被隐藏,因此,如果窗口大小增加,从那里开始,带有自定义滚动的块将不会显示,因为它无法计算当前的宽度和高度。 if i refresh the page, it works fine. 如果我刷新页面,效果很好。 is there anything i can work out to refresh this particular block without reloading the page? 有什么我可以解决的问题来刷新此特定块而无需重新加载页面? http://narkosi.com/test_copy here is the link to the fresh copy of the site. http://narkosi.com/test_copy此处是指向该网站的新副本的链接。 try resizing it to mobile view and back to desktop version, you will notice right and left column hidden but however if refreshed the page, it works fine. 尝试将其大小调整为移动视图并返回到桌面版本,您会注意到左右列处于隐藏状态,但是如果刷新页面,它将可以正常工作。 Thank you in advance. 先感谢您。

Try $( window ).resize(); 尝试$( window ).resize(); function it will run when you viewport is change 当您更改视口时它将运行的功能

$( window ).resize(function() {
  your code
});

for more details check https://api.jquery.com/resize/ 有关更多详细信息,请检查https://api.jquery.com/resize/

for plain javascript use onresize="myFunction()" on body for more details check http://www.w3schools.com/jsref/event_onresize.asp 有关纯JavaScript的信息,请onresize="myFunction()" on body使用onresize="myFunction()" on body了解更多详细信息,请访问http://www.w3schools.com/jsref/event_onresize.asp

If above code is not working try 如果上面的代码不起作用,请尝试

$([document, window]).on('ready resize', function (e) {
alert("hi");
});

The answer is extually exist in the plugin's docs 答案完全存在于插件的文档中

General API Information (from the docs) 常规API信息(来自文档)

jcf.replaceAll( [context] ) - Replace elements on the whole page. jcf.replaceAll( [context] ) -替换整个页面上的元素。 Optional argument is context to use (can be CSS selector, or DOM/jQuery object). 可选参数是要使用的上下文(可以是CSS选择器,也可以是DOM / jQuery对象)。 Add class jcf-ignore on the element to prevent its customization. 在元素上添加类jcf-ignore以防止其自定义。

jcf.replace( elements [, moduleName] [, customOptions] ) - Replace certain element or multiple elements jcf.replace( elements [, moduleName] [, customOptions] ) -替换某些元素或多个元素

jcf.destroyAll( [context] ) - Destroy all custom form elements instances and restore original form elements. jcf.destroyAll( [context] ) -销毁所有自定义表单元素实例并还原原始表单元素。 Optional argument is context to use (can be CSS selector, or DOM/jQuery object). 可选参数是要使用的上下文(可以是CSS选择器,也可以是DOM / jQuery对象)。

jcf.destroy( elements ) - Destroy certain element or multiple form elements. jcf.destroy( elements ) -销毁某些元素或多个表单元素。 Should be applied to native form controls. 应该应用于本机表单控件。

So you can just destroy and do replace again (I tried to do it on your site and it works). 这样您就可以destroy并再次replace (我曾尝试在您的网站上这样做并且可以工作)。

$(window).resize(function() {
    /* I do this for all elements 
       but it's better for you to refresh only the elements you need to */
    setTimeout(function() {
       jcf.destroyAll();
       jcf.replaceAll();
    }, 0);
});

Why the setTimeout needed? 为什么需要setTimeout Well, It doesn't work without it ;) 好吧,没有它是行不通的;)

A good explanation for this you can find in this question - Why is setTimeout(fn, 0) sometimes useful? 您可以在以下问题中找到对此的很好解释- 为什么setTimeout(fn,0)有时有用?

In short: 简而言之:

The solution is to "pause" the JavaScript execution to let the rendering threads catch up. 解决方案是“暂停” JavaScript执行,以使渲染线程赶上来。 And this is the effect that setTimeout() with a timeout of 0 does 这就是setTimeout()的超时值为0的效果

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

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