【问题标题】:How do I add a custom icon to a standard jQuery UI theme?如何将自定义图标添加到标准 jQuery UI 主题?
【发布时间】:2011-04-19 16:07:17
【问题描述】:

使用标准图标集中提供的图标之一很容易:

$("#myButton").button({icons: {primary: "ui-icon-locked"}});

但是,如果我想添加自己的一个不属于框架图标集的图标怎么办?

我认为这就像给它自己的带有背景图像的 CSS 类一样简单,但这不起作用:

.fw-button-edit {
    background-image: url(edit.png);
}

有什么建议吗?

【问题讨论】:

    标签: jquery-ui button icons


    【解决方案1】:

    在css中

    .ui-button .ui-icon.custom-class {
        background-image: url(your-path-to-normal-image-file.png);
        width: your-icon-width;
        height: your-icon-height; 
    }
    
    .ui-state-active .ui-icon.custom-class, .ui-button:active .ui-icon.custom-class {
        background-image: url(your-path-to-highlighted-image-file.png);
        width: your-icon-width;
        height: your-icon-height; 
    }
    

    在 HTML 中

    <button type="button" class="ui-button ui-widget ui-corner-all">
        <span class="custom-class"></span> CAPTION TEXT
    </button>
    

    在 JavaScript 中

    $("selector").button({
        icons: { primary: "custom-class" }
    });
    

    【讨论】:

    • 您的回答将受益于解释。
    【解决方案2】:

    我也可以推荐:

    .ui-button .ui-icon.your-own-custom-class {
        background-image: url(your-path-to-normal-image-file.png);
        width: your-icon-width;
        height: your-icon-height; 
    }
    
    .ui-button.ui-state-hover .ui-icon.your-own-custom-class {
        background-image: url(your-path-to-highlighted-image-file.png);
        width: your-icon-width;
        height: your-icon-height; 
    }
    

    然后只需输入 JS 代码:

            jQuery('selector-to-your-button').button({
            text: false,
            icons: {
                primary: "you-own-cusom-class"   // Custom icon
            }});
    

    它对我有用,希望它也对你有用!

    【讨论】:

    • 谢谢!这是否记录在 jquery-ui 文档中?我在任何地方都找不到它。
    • 引用 you-own-cusom-class 怎么会有人让它工作?尽管如此,领先还是不错的。
    • 后期编辑——但对我来说 CSS 的 width 和 height 属性似乎完全无效。
    【解决方案3】:

    此链接上的解决方案对我很有用: http://www.jquerybyexample.net/2012/09/how-to-assign-custom-image-to-jquery-ui-button.html

    $(document).ready(function() {
        $("#btnClose")
        .text("")
        .append("<img height="100" src="logo.png" width="100" />")
        .button();
    });​
    

    【讨论】:

    • 我不得不在追加中使用单引号...但是很简单而且很有效...
    【解决方案4】:
    // HTML
    
    <div id="radioSet" style="margin-top:4px; margin-left:130px;" class="radio">
          <input type="radio" id="apple" name="radioSet"  value="1"><label for="apple">Apple</label>
          <input type="radio" id="mango" name="radioSet" value="2"><label for="mango">Mango</label>
    </div>
    
    
    // JQUERY
    
    // Function to remove the old default Jquery UI Span and add our custom image tag
    
        function AddIconToJQueryUIButton(controlForId)
        {
            $("label[for='"+ controlForId + "'] > span:first").remove();
            $("label[for='"+ controlForId + "']")
            .prepend("<img position='fixed' class='ui-button-icon-primary ui-icon' src='/assets/images/" + controlForId + ".png' style=' height: 16px; width: 16px;' />");
    
        }
    
    // We have to call the custom setting to happen after document loads so that Jquery UI controls will be there in place
    
       // Set icons on buttons. pass ids of radio buttons
       $(document).ready(function () {
                     AddIconToJQueryUIButton('apple');   
                     AddIconToJQueryUIButton('mango');   
        });
    
       // call Jquery UI api to set the default icon and later you can change it        
        $( "#apple" ).button({ icons: { primary: "ui-icon-gear", secondary: null } });
        $( "#mango" ).button({ icons: { primary: "ui-icon-gear", secondary: null } });
    

    【讨论】:

      【解决方案5】:

      在 msanjay 回答的基础上,我将此扩展为适用于 jquery ui 按钮和单选按钮的自定义图标:

      <div id="toolbar">
          <button id="btn1" data-img="/images/bla1.png">X</button>
          <span id="radioBtns">
            <input type="radio" id="radio1" name="radName" data-mode="scroll" data-img="Images/bla2.png"><label for="radio1">S</label>
            <input type="radio" id="radio2" name="radName" data-mode="pan" data-img="Images/bla3.png"><label for="radio2">P</label>
          </span>
      </div>    
      
      $('#btn1').button();
      $('#radioBtns').buttonset();
      
      loadIconsOnButtons('toolbar');
      
      function loadIconsOnButtons(divName) {
          $("#" + divName + " input,#" + divName + "  button").each(function() {
            var iconUrl = $(this).attr('data-img');
            if (iconUrl) {
              $(this).button({
                text: false,
                icons: {
                  primary: "ui-icon-blank"
                }
              });
              var imageElem, htmlType = $(this).prop('tagName');
              if (htmlType==='BUTTON') imageElem=$(this);
              if (htmlType==='INPUT') imageElem=$("#" + divName + " [for='" + $(this).attr('id') + "']");
              if (imageElem) imageElem.css('background-image', "url(" + iconUrl + ")").css('background-repeat', 'no-repeat');
            }
          });
      }
      

      【讨论】:

        【解决方案6】:

        我向 JQuery UI 添加自定义图标的解决方案(使用精灵):

        CSS:

        .icon-example {
            background-position: 0 0;
        }
        
        .ui-state-default .ui-icon.custom {
            background-image: url(icons.png);
        }
        

        .icon-example 定义图标在自定义图标文件中的位置。 .ui-icon.custom 使用自定义图标定义文件。

        注意:您可能还需要定义其他 JQuery UI 类(如 .ui-state-hover)。

        JavaScript:

        $("selector").button({
            icons: { primary: "custom icon-example" }
        });
        

        【讨论】:

        • 你的答案难以理解
        【解决方案7】:

        这是基于以上 Yijiang 和 Panayiotis 提供的信息,以及jquery ui button sample code:

        由于我正在迁移早期的 JSP 应用程序,该应用程序的工具栏带有每个按钮的图像,我希望将图像包含在按钮声明本身中,而不是为每个工具栏按钮创建一个单独的类。

        <div id="toolbarDocs" class="tableCaptionBox">
            <strong>Checked Item Actions: </strong>
        
            <button id="btnOpenDocs" data-img="<s:url value="/images/multi.png"/>">Open Documents</button>
            <button id="btnEmailDocs" data-img="<s:url value="/images/email.png"/>">Attach to Email</button>
        </div>
        

        当然,除了上面的两个之外,还有很多按钮。上面的 s 标签是一个 struts2 标签,但你可以用任何 URL 替换它

                <button id="btnOpenDocs" data-img="/images/multi.png">Open Documents</button>
        

        以下脚本从按钮标签中查找属性 data-img,然后将其设置为按钮的背景图像。

        它会临时设置 ui-icon-bullet(任何现有的任意样式),然后再更改。

        这个类定义了临时样式(如果您打算使用它,最好为特定工具栏添加更多选择器,以便页面的其余部分不受影响)。实际图像将被以下 Javascript 替换:

        button.ui-button .ui-icon {
            background-image: url(blank.png);
            width: 0;
            height: 0; 
        }
        

        以及以下 Javascript:

         $(document).ready(function () {
            $("#toolbarDocs button").each(
                  function() { 
                    $(this).button(
                      { text: $(this).attr('data-img').length === 0? true: false, // display label for no image
                       icons: { primary: "ui-icon-bullet"  }
                      }).css('background-image', "url(" + $(this).attr('data-img') +")")
                       .css('background-repeat', 'no-repeat');
                    });
          });
        

        【讨论】:

        • 这个版本适合我,因为它允许我使用使用 URL 而不是 CSS 类定义的图像。
        • data- 属性是 HTML 5 的一部分,但正如 SO link 所示,它也适用于早期的浏览器。我刚刚发现 jquery 在内部使用了它,并且 jquery 有一个 data() 方法。我们可以使用方法 .data('img') 代替 .attr('data-img')
        【解决方案8】:

        我相信他不起作用的原因是因为你的图标的 background-image 属性被 jQuery UI 默认精灵图标背景图像覆盖。有问题的样式是:

        .ui-state-default .ui-icon {
            background-image: url("images/ui-icons_888888_256x240.png");
        }
        

        这比您的.fw-button-edit 选择器具有更高的特异性,因此会覆盖背景图像属性。由于他们使用精灵,.ui-icon-locked 规则集仅包含获取精灵图像位置所需的background-position。我相信使用它会起作用:

        .ui-button .ui-icon.fw-button-edit {
            background-image: url(edit.png);
        }
        

        或其他具有足够特异性的东西。在此处了解有关 CSS 特异性的更多信息:http://reference.sitepoint.com/css/specificity

        【讨论】:

        • 非常感谢您,好心的先生。
        • Jquery UI 不将背景应用到插入元素中的跨度吗?
        猜你喜欢
        • 1970-01-01
        • 2023-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-13
        相关资源
        最近更新 更多