简体   繁体   English

jQuery-Mobile外部面板始终在页面之间打开

[英]jQuery-Mobile external Panel always open among pages

I've tried to search around but found no relatable topics I could understand. 我尝试搜索,但是没有找到我能理解的相关主题。 I'm not very familiar with Javascript or coding in general. 我对Javascript或一般的编码不是很熟悉。

I am using Jquery Mobile 1.4.5 and this is my issue: 我正在使用Jquery Mobile 1.4.5,这是我的问题:

I cannot get external panels to work properly. 我无法使外部面板正常工作。 The panel displays just fine on my first page, but when I change page it won't show up as intended. 该面板在我的第一页上显示得很好,但是当我更改页面时,它不会按预期显示。 My plan is to have the panels work in the same manner as they do on the Jquery mobile demo page. 我的计划是使面板以与Jquery移动演示页面相同的方式工作。

Link: Jquery Mobile Demo 链接: jQuery Mobile演示

Here you can see the panel is always showing no matter what page they are on, I found out they don't use external panels on that site but it should still be possible. 在这里,您可以看到面板始终显示,无论它们位于哪个页面上,我都发现他们不在该站点上使用外部面板,但仍然可以。

How my site works at the moment: 我的网站目前的运作方式:

  1. Panel work just fine when loading first page (#page_home) 加载首页(#page_home)时面板工作正常
  2. When entering new page (#page_kodi or #page_download) it does not show up automatically as intended. 进入新页面(#page_kodi或#page_download)时,它不会按预期自动显示。
  3. When I enter #page_kodi or #page_download and manually bring it up it stays up as intended 当我输入#page_kodi或#page_download并手动将其调高时,它会保持原状
  4. This is the odd part: When I go from (with panel open) #page_download to #page_kodi to #page_home (main page) it works. 这是奇怪的部分:当我从(打开面板的情况)从#page_download到#page_kodi到#page_home(主页)时,它可以工作。
  5. when I go from #page_home to another page it does not work. 当我从#page_home转到另一个页面时,它不起作用。

Here is my JS code for panels, I'm sure there is a better way to write this, and maybe some of it is not needed. 这是我的面板JS代码,我敢肯定有一种更好的方式编写此代码,也许其中一些不需要。

Javascript: Javascript:

    <script>
    <!-- Creates the panels & navbars/Tabs -->
    $(document).on("pagecreate", function() {
        $("body > [data-role='panel']").panel();
        $("body > [data-role='panel'] [data-role='listview']").listview();
    });
    $(document).on("pageshow", function() {
        $("body > [data-role='header']").toolbar();
        $("body > [data-role='header'] [data-role='navbar']").navbar();
    });
    </script>

<script>
$(document).on("pagebeforeshow", function( event, data ) {
    $('#leftpanel').panel("open");
})
</script>

<script>
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
$(document).on("pagebeforecreate", "#page_home", function () {
  $( "#leftpanel" ).panel({ dismissible: true });
  $( "#leftpanel").panel("close");
});
}
</script>

<script>
$(document).on("pagecreate", "#page_home", function () {
 if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
 setTimeout(function(){
    $('#leftpanel').panel("close");
}, 500);
 }
});
</script>

<script>
$(document).on("pagebeforecreate", "#page_home", function () {
  $( "#leftpanel" ).panel({ dismissible: false });
});
</script>

<script>
$(document).on("pagebeforecreate", "#page_download", function () {
  $( ".leftpanel" ).panel( "option", "dismissible", false );
  $('#leftpanel').panel("open");
});
</script>

<script>
$(document).on("pagebeforecreate", "#page_kodi", function () {
  $( "#leftpanel" ).panel( "option", "dismissible", false );
  $('#leftpanel').panel("open");
});
</script>

<script>
/* Left & Right swipe gestures to open panels*/
$(document).on("pagecreate", function() {
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
    $(document).on("swipeleft swiperight", function(e) {
        if ($(".ui-page-active").jqmData("panel") !== "open") {
            if (e.type === "swipeleft") {
                $("#rightpanel").panel("open");
            } else if (e.type === "swiperight") {
                $("#leftpanel").panel("open");
            }
        }
    });
    }
});
</script>

I have placed all these in my HTML file. 我已经将所有这些放置在我的HTML文件中。

HTML Panel: HTML面板:

<div style="margin-top: 0px; background-color: #212120;" class="customlist panel-open" data-role="panel" data-position="left" data-dismissible="" data-display="overlay" data-theme="none" id="leftpanel">
    <ul data-role="listview" data-inset="false">
MY CONTENT HERE
</ul>
</div>

data-dismissible="" I have put it this way because that's what works when you set it manually with JS, or so i've read. data-dismissible =“”我这样说是因为当您使用JS手动设置它时,这就是可行的方法,或者我已经读过。 It did not work if I set it to false or true. 如果将其设置为false或true,它将无法正常工作。

Basically what I'm trying to do here is always have the panel OPEN on bigger screens and closed with option to open it with swipe on smaller screens. 基本上,我要在此处执行的操作始终是在较大的屏幕上将面板打开,然后在较小的屏幕上通过滑动选项将其关闭。 This works as of now. 截止目前 The trouble I am having is when changing pages the panel does not act as intended and closes when I am going from my front page to another, but not if I go from another to my front page. 我遇到的麻烦是,在更改页面时,面板无法正常工作,而当我从首页转到另一页时,该面板关闭,但如果从另一页转到首页,则无法关闭面板。

PS: I've also put the panel between two of my pages like this: PS:我也将面板放置在我的两个页面之间,如下所示:

page_home page_home

-- panel -- some popup -面板-一些弹出窗口

page_download page_download

page_kodi page_kodi

Thanks in advance for all the help you can give and sorry for the wall of text. 在此先感谢您提供的所有帮助,并在此致歉。

Assuming you are using a single-page model, here is a simple stub for a JQM project with a Panel which can stay open among different pages. 假设您使用的是单页模型,这是一个带有Panel的JQM项目的简单存根,可以在不同页面之间保持打开状态。 The default behavior is parametrized by overriding the _bindPageEvents function of the mobile.panel widget, so you can dinamcally set a flag for that. 通过覆盖mobile.panel小部件的_bindPageEvents函数来对默认行为进行参数化,因此您可以为此专门设置一个标志。

You can set the stayAlwaysOpen flag as you like, by spoofing the useragent string or (maybe better) by checking the viewport width, up to you. 您可以根据stayAlwaysOpen通过欺骗useragent字符串或(视情况而定)检查视口宽度来设置stayAlwaysOpen标志,具体取决于您自己。 You could also check a CSS breakpoint for that purpose. 您也可以为此目的检查CSS断点。

To keep header navigation and make the panel somewhat more pleasant, i used also the function scalePanelToContent from: jQuery mobile panel between header and footer (credits: Omar ). 为了保持标题导航并使面板更加舒适,我还使用了来自以下位置的jQuery移动面板中的 scalePanelToContent函数(信贷: Omar )。

 var stayAlwaysOpen = true; $.widget("mobile.panel", $.mobile.panel, { _bindPageEvents: function() { var self = this; this.document // Close the panel if another panel on the page opens .on("panelbeforeopen", function(e) { if (self._open && e.target !== self.element[0]) { self.close(); } }) // On escape, close? might need to have a target check too... .on("keyup.panel", function(e) { if (e.keyCode === 27 && self._open) { self.close(); } }); if (!this._parentPage && this.options.display !== "overlay") { this._on(this.document, { "pageshow": function() { this._openedPage = null; this._getWrapper(); } }); } // Clean up open panels after page hide if(stayAlwaysOpen) return; if (self._parentPage) { this.document.on("pagehide", ":jqmData(role='page')", function() { if (self._open) { self.close( true ); } }); } else { this.document.on("pagebeforehide", function() { if (self._open) { self.close( true ); } }); } } }); function scalePanelToContent() { var screenH = $.mobile.getScreenHeight(); var headerH = $(".ui-header").outerHeight() - 1; var footerH = $(".ui-footer").outerHeight() - 1; var panelH = screenH - headerH - footerH; $(".ui-panel").css({ "top": headerH, "bottom": footerH, "min-height": panelH }); } $(document).ready(function() { $("[data-role='header'], [data-role='footer']").toolbar({ theme: "a", position: "fixed", tapToggle: false }); $("#nav-panel").panel({ theme: "b", display: "overlay", position: "left", positionFixed: true, swipeClose: false, dismissible: false }).enhanceWithin(); $("#nav-panel").on("panelbeforeopen", function(event, ui) { scalePanelToContent(); $(".ui-content").animate({ "margin-left": "17em" }, 300, "swing"); }); $("#nav-panel").on("panelbeforeclose", function(event, ui) { $(".ui-content").removeClass("panel-shrink").animate({ "margin-left": "0" }, 300, "swing", function() { $(this).removeAttr("style"); }); }); scalePanelToContent(); }); $(window).on("resize", function() { scalePanelToContent(); }); $(document).on("pagecontainerbeforeshow", function(e, ui) { var isPanelOpen = $("#nav-panel").hasClass("ui-panel-open"); $(".ui-content").toggleClass("panel-shrink", isPanelOpen); }); 
 .panel-shrink { margin-left: 17em !important; } 
 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css"> <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script> <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script> </head> <body> <div data-role="header"> <a href="#nav-panel" data-icon="bars" data-iconpos="notext">Menu</a> <h2>Header</h2> </div> <div data-role="footer"> <h2>Footer</h2> </div> <div data-role="page" id="page-1"> <div data-role="content"> <form> <fieldset data-role="controlgroup"> <legend>Vertical:</legend> <input name="checkbox-v-2a" id="checkbox-v-2a" type="checkbox"> <label for="checkbox-v-2a">One</label> <input name="checkbox-v-2b" id="checkbox-v-2b" type="checkbox"> <label for="checkbox-v-2b">Two</label> <input name="checkbox-v-2c" id="checkbox-v-2c" type="checkbox"> <label for="checkbox-v-2c">Three</label> </fieldset> </form> </div> </div> <div data-role="page" id="page-2"> <div data-role="content"> <ul data-role="listview" data-inset="true"> <li><a href="#">Acura</a></li> <li><a href="#">Audi</a></li> <li><a href="#">BMW</a></li> <li><a href="#">Cadillac</a></li> <li><a href="#">Ferrari</a></li> </ul> </div> </div> <div data-role="page" id="page-3"> <div data-role="content"> Page 3 </div> </div> <div data-role="panel" id="nav-panel"> <ul data-role="listview"> <li><a href="#page-1">Link #1</a> </li> <li><a href="#page-2">Link #2</a> </li> <li><a href="#page-3">Link #3</a> </li> </ul> </div> </body> </html> 

Just a last hint: 最后提示:

You can use the same stayAlwaysOpen flag inside your swipe events so that the panel will keep the same behavior in mobile devices and in desktop browsers also for smaller window sizes. 您可以在滑动事件中使用相同的stayAlwaysOpen标志,以使面板在移动设备和桌面浏览器中也保持相同的行为,也适用于较小的窗口。

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

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