【问题标题】:Button auto size in Jquery MobileJquery Mobile中的按钮自动大小
【发布时间】:2013-04-27 05:41:23
【问题描述】:

我正在开发一个 jquery/PhoneGap 应用程序。我一直在努力让按钮按照我想要的方式运行。简而言之,我正在努力实现以下目标:

  1. 我放置了一组六个 Jquery-Mobile 按钮(迷你、普通或按钮组)。
  2. 上面的集合需要在一条水平线上,所以我把它们放在了一个 div 中。

按钮的数量及其文本是动态变化的,所以我需要一个 CSS/JS 技巧,允许我根据 div/屏幕大小调整按钮大小和文本大小。当我开始使用 Jquery mobile 时(两周前),我认为这将是一个基本功能 :) 但是唉!

我现在正在尝试的一些代码是:

//TO CREATE BUTTONS
for(var button_id=0; button_id < window.g_maxLength/2; button_id++){
var bt_id= "<button  class =\"tile\" data-theme=\"e\" data-inline=\"true\" data-mini=\"true\" id =\"button_tid"+button_id+"\";>.</button>";
$("#buttoncontainer1").append($(bt_id)); 
}

//HTML
<div id="tiled" align="center">
            <div data-role="controlgroup" data-type="horizontal" id="buttoncontainer1">
        <!-- Button will be added by JS-->
        </div>
</div>
//CSS
#tiled {

align:center; 
height:23%; 
position:absolute;
text-align :center;
padding: 1px;
width:90%;
top:73%;
margin-right:4%;
margin-left:4%; 
background-color:#b0e0e6;   
border-radius: 10px;
border-width: 3%;
border-style:double;

现在我所拥有的在小屏幕设备上运行良好,但是一旦我在大屏幕设备上打开我的应用程序,按钮看起来非常小,并且有很多空白空间。在这里的任何帮助将不胜感激!

PS:也使用了媒体查询 - 但不知何故,它们在 jquery-mobile 上不起作用。

@media (min-width: 500px) {
            html { font-size: 120%; }
        }

【问题讨论】:

  • 您可以使用ui-grid 布局执行与fiddle.jshell.net/Palestinian/DTPZx 类似的操作。在 JQM 自定义 CSS 中,每个属性都应以 !important 结尾以覆盖 JQM 样式。
  • 谢谢,如果我能弄清楚字体大小问题,这通常可以工作:)。虽然它不适用于按钮组。
  • 是的,我错过了控制组的事情。
  • 查看jsfiddle.net/Palestinian/UYa4Y,如果您有任何意见,请告诉我。更改“结果”窗口大小并点击“运行”。
  • 这正是我需要的@Omar。谢谢,看起来很复杂,但我会解构它:)。

标签: javascript css jquery-mobile button


【解决方案1】:

这是自动调整按钮宽度和字体大小的解决方法。

演示:http://jsfiddle.net/Palestinian/UYa4Y/

// Number of buttons
var buttons = $('[data-   role=controlgroup]').find('a').length;
// Parent div width
var btn_width = $('#tiled').width() / buttons;

// Remove left/right button padding
$('.ui-btn-inner').css({
 'padding-left': 1,
    'padding-right': 1
});

// Set button new width
$('.ui-btn-inner').width(btn_width - 4);

// Adjust font-size for each button based on text
$('.ui-btn-text').each(function () {
 while ($(this).width() > $('.ui-btn-inner').width()) {
    var font = parseFloat($(this).css('font-size')) - 1 + "px";
    $(this).css('font-size', font);
 }
});

【讨论】:

  • 这似乎是一个非常简单的用例,但是 jquery 需要大量的努力和黑客才能做到这一点。非常感谢有趣的例子,这些也将帮助我在其他情况下破解 Jquery。
  • @Naunidh 我很高兴我能帮上忙 :) 嗯,JQM 有时需要一些老生常谈的方法 ;)
猜你喜欢
  • 2013-07-15
  • 1970-01-01
  • 1970-01-01
  • 2012-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多