简体   繁体   English

如何在网页上启用/禁用(实时)javascript插件?

[英]How can I enable/disable(in real time) javascript plugins on a webpage?

I need to enable/disable .js plugins in real time. 我需要实时启用/禁用.js插件。 When user re-sizes his browser window, desktop version becomes mobile. 当用户调整浏览器窗口大小时,台式机版本变为移动版。 The thing is I need to disable some javascript ( fullpage.js ) and add some css. 问题是我需要禁用一些javascript( fullpage.js )并添加一些CSS。 And there is no problems if you reload the page or use full-screen window on your pc, from start to the end. 从头到尾重新加载页面或在PC上使用全屏窗口都没有问题。 The question is : how can I disable fullpage.js on width < 800 ? 问题是:如何禁用宽度小于800的fullpage.js? I've tried this things : 我已经尝试过这些东西:

Added this function to body onresize event (no result without reloading) : 将此函数添加到body onresize事件(不重新加载就不会有结果):

 function checkWidth() {
        if($(window).width() <= 760 ){
            $('#menu-header-button').click(function(){
                $('#navigation').toggleClass('navbar-v');  
                $('#logo-mobile').toggleClass('visible');
                $('#menu-header-button').addClass('disabled');
                 setTimeout(function(){ 
                    $('#menu-header-button').removeClass('disabled');  
                 }, 500);
            });
        } else if($(window).width() > 760 ){
          $('#fullpage').fullpage({
              menu: '#menu',
              anchors: ['firstPage', 'secondPage', '3rdPage', '4thPage'],
              css3: true,
              afterLoad: function(anchorLink, index){
              if( anchorLink == 'firstPage'){
                   $('#navigation').removeClass('navbar-v'); 
              }
          },
              onLeave: function(index, nextIndex, direction){
              setTimeout(function(){ 
                    $('#navigation').addClass('navbar-v'); 
              }, 500); 
              }
            });
        }

} }

This was working only sometimes : 这仅在某些时候有效:

$(window).onresize= checkWidth();
function checkWidth() {
                    if($(window).width() == 761 ){
                        location.reload(true);
                    }
                    //...
}

This wasn't working at all : 这根本没有用:

   $(window).onresize= checkWidth();
    function delayCheckWidth(){
      setTimeout(function(){ 
                              checkWidth(); 
                           }, 50); //I thought some delay time can be useful here 
    }
    function checkWidth() {
                        if($(window).width() == 761 ){
                            location.reload(true);
                        }
                        //...
    }

Related themes i checked out : 我签出的相关主题:

  1. Throttling 节流
  2. How to disable / Enable plugins? 如何禁用/启用插件?
  3. Enable / Disable JavaScript code 启用/禁用JavaScript代码
  4. How to disable / Enable plugins? 如何禁用/启用插件?
  5. Enable and disable jquery knob dynamically 动态启用和禁用jquery旋钮
  6. Disable and enable jQuery context menu 禁用和启用jQuery上下文菜单
  7. Lots of others. 还有很多。

Nothing about real time or nothing interesting/helpful . 没什么关于实时的,也没有什么有趣/无助的。

Do you have any advises? 您有什么建议吗? Maybe i don't need to go this exactly this way ? 也许我不需要这样走吗?

Have you tried the responsive option of fullPage.js? 您是否尝试过fullPage.js的responsive选项? Not sure if that's what you want or your really need to destroy fullPage.js completely. 不知道这是您想要的还是真的需要完全破坏fullPage.js。

From fullPage.js documentation : fullPage.js文档中

responsive: (default 0) A normal scroll (autoScrolling:false) will be used under the defined width in pixels. 响应:(默认值0)将在定义的宽度(以像素为单位)下使用正常滚动(autoScrolling:false)。 A class fp-responsive is added to the plugin's container in case the user wants to use it for his own responsive CSS. 如果用户希望将fp响应类用于其自己的响应CSS,则将其添加到插件的容器中。 For example, if set to 900, whenever the browser's width is less than 900 the plugin will scroll like a normal site. 例如,如果设置为900,则每当浏览器的宽度小于900时,插件就会像正常站点一样滚动。

The normal scrolling in fullPage.js will keep sections height as well as the events assigned to the menu, or the control arrows for the horizontal sliders, but it will scroll normally as any website. 在fullPage.js中正常滚动将保持节的height以及分配给菜单的事件或水平滑块的控制箭头,但它将像在任何网站上一样正常滚动。 You can see a demo of that mode here . 您可以在此处查看该模式的演示。

In any case, if you really want to destroy fullPage.js for any reason, you would need to use the provided method for it. 无论如何,如果您确实出于任何原因想要破坏fullPage.js,则需要使用提供的方法。 From the docs: 从文档:

$.fn.fullpage.destroy(type) $ .fn.fullpage.destroy(类型)

Destroys the plugin events and optinally its HTML markup and styles. 销毁插件事件,并销毁其HTML标记和样式。 Ideal to use when using AJAX to load content. 使用AJAX加载内容时的理想选择。 () ()

type: can be 'empty' or 'all' . 类型:可以为'empty''all' If all is passed, the HTML markup and styles used by fullpage.js will be removed. 如果全部通过,则将删除fullpage.js使用的HTML标记和样式。 This way the original HTML markup, the one used before any plugin modification is made, will be maintained. 这样,将保留原始HTML标记(在进行任何插件修改之前使用的标记)。

//destroy any plugin event (scrolls, hashchange in the URL...)
$.fn.fullpage.destroy();

//destroy any plugin event and any plugin modification done over your original HTML markup.
$.fn.fullpage.destroy('all');

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

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