当 jquerymobile 中的窗口大小更改时更改 div id 和属性

2021-12-27 00:00:00 php javascript jquery-mobile

我当前的代码是

测试

当窗口大小小于640px时,我如何将其更改为:

测试

data-role="panel" 是 jquerymobile 代码.问题集中在我们如何将 data-role="panel" 属性添加到 div 上.谢谢!

您可以在 http://jsbin.com/wakagumu/11/edit.如果成功,将 id="column-left" 更改为 data-role="panel" id="left-panel" 后,测试FIRST"将消失.

解决方案

更改属性不会将div转换为面板,您需要初始化 手动.在 jQuery Mobile 1.3 中,您应该在动态附加 panel 时使用 .trigger("pagecreate") 以初始化它.

下面的解决方案创建一个面板并在页面的宽度较小时移动内容 div的元素;它删除 panel 并将 content div 的元素返回到它们的原始位置.此外,它会在 header 内创建一个按钮来打开 面板.它可以用于任何页面事件以及窗口的throttledresizeorientationchange.

$(window).on("throttledresize", function () {var activePage = $.mobile.activePage;if ($(window).width() <500 && activePage.find("[data-role=panel]").length === 0) {/* 创建按钮 */var button = $("", {"数据角色": "按钮","数据图标": "条","id": "panelBtn","数据主题": "e",课程:ui-btn-left"}).text("面板");/* 添加点击监听器以打开面板并将其附加到标题 */activePage.find(".ui-header").append($(button).on("click", function () {$("#left-panel").panel("open");}));/* 保存菜单 */var menu = $("#menu");/* 创建一个面板附加菜单创建页面 */activePage.prepend($("

", {id:左面板",数据角色":面板",数据位置":左",数据显示":推送"}).append($("

", {课程:ui-panel-inner"}).append(menu))).trigger("pagecreate");}if ($(window).width() > 500 && activePage.find("[data-role=panel]").length === 1) {/* 移除面板和按钮返回菜单到内容 div */如果(activePage.hasClass(ui-page-panel-open")){activePage.find("[data-role=panel]").panel("close").on("panelclose", function () {var menu1 = activePage.find("[数据角色=面板] #menu");activePage.find("[data-role=content]").append(menu1);activePage.find("[数据角色=面板]").remove();activePage.find("#panelBtn").remove();activePage.trigger("pagecreate");});} 别的 {var menu1 = activePage.find("[数据角色=面板] #menu");activePage.find("[data-role=content]").append(menu1);activePage.find("[数据角色=面板]").remove();activePage.find("#panelBtn").remove();activePage.trigger("pagecreate");}}});

<块引用>

演示

My current code is

<div id="column-left">
     Test
</div>

When the window size is smaller than 640px, how can I change it as:

<div data-role="panel" id="left-panel" data-position="left">
     Test
</div>

data-role="panel" is jquerymobile code. The question is focusing on how we can add the data-role="panel" attribute to the div. Thanks!

You may test your code in http://jsbin.com/wakagumu/11/edit. If it success, the test "FIRST" will disappear after changing the id="column-left" to data-role="panel" id="left-panel".

解决方案

Changing attributes won't convert a div into a panel, you need to initialize it manually. In jQuery Mobile 1.3, you should use .trigger("pagecreate") when appending a panel dynamically in order to initialize it.

The below solution creates a panel and moves content div's elements when page's width is small; and it removes panel and returns content div's element to their original position. Also, it creates a button inside header to open the panel. It can be used in any page events as well as on window's throttledresize and orientationchange.

$(window).on("throttledresize", function () {
    var activePage = $.mobile.activePage;
    if ($(window).width() < 500 && activePage.find("[data-role=panel]").length === 0) {
       /* create button */
        var button = $("<a/>", {
            "data-role": "button",
                "data-icon": "bars",
                "id": "panelBtn",
                "data-theme": "e",
            class: "ui-btn-left"
        }).text("Panel");
        /* add click listener to open panel 
           and append it to header         */
        activePage.find(".ui-header").append($(button).on("click", function () {
            $("#left-panel").panel("open");
        }));

        /* save menu */
        var menu = $("#menu");
        /* create a panel 
           append menu
           create page    */
        activePage.prepend($("<div/>", {
            id: "left-panel",
                "data-role": "panel",
                "data-position": "left",
                "data-display": "push"
        }).append($("<div/>", {
            class: "ui-panel-inner"
        }).append(menu))).trigger("pagecreate");
    }

    if ($(window).width() > 500 && activePage.find("[data-role=panel]").length === 1) {
        /* remove panel and button
           return menu to content div */
        if (activePage.hasClass("ui-page-panel-open")) {
            activePage.find("[data-role=panel]").panel("close").on("panelclose", function () {
                var menu1 = activePage.find("[data-role=panel] #menu");
                activePage.find("[data-role=content]").append(menu1);
                activePage.find("[data-role=panel]").remove();
                activePage.find("#panelBtn").remove();
                activePage.trigger("pagecreate");
            });
        } else {
            var menu1 = activePage.find("[data-role=panel] #menu");
            activePage.find("[data-role=content]").append(menu1);
            activePage.find("[data-role=panel]").remove();
            activePage.find("#panelBtn").remove();
            activePage.trigger("pagecreate");
        }
    }
});

Demo

相关文章