2010-10-01 81 views
43

这是很容易利用现有的图标从一个标准的图标集:如何将自定义图标添加到标准jQuery UI主题?

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

但是,如果我想补充我自己的图标是不是框架图标集的第一部分是什么?

我认为这将是为给它一个背景图片自己的CSS类一样简单,但是,这并不工作:

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

有什么建议?

回答

63

我也可以推荐:

.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 
     }}); 

它为我,希望它为你的作品太!

+0

这对我有效。注意你也可以使用精灵。 – 2011-06-10 02:05:58

+1

@RobOsborne你如何使用精灵?它不适用于我 – 2013-08-27 16:48:44

+0

谢谢!这是记录在jquery-ui文档?我无法在任何地方找到它。 – shimizu 2017-07-10 16:43:07

14

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

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

这有比你.fw-button-edit选择较高的特异性,从而覆盖背景图像proerty。由于它们使用精灵,.ui-icon-locked规则集仅包含获取精灵图像位置所需的background-position。我相信使用这将工作:

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

或其他具有足够的特异性的东西。了解更多有关CSS具体位置:http://reference.sitepoint.com/css/specificity

+0

非常感谢你,善良的先生。 – Brett 2010-10-01 03:15:34

+0

不是JQuery UI将背景应用于插入元素的跨度? – UpTheCreek 2010-12-02 15:29:05

+0

@UpTheCreek是的,是吗? – 2010-12-02 15:31:03

3

这是基于由姜毅及以上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> 

以下脚本查找从按钮标记的属性数据IMG,然后设置一个作为背景图片更换按钮。

它暂时设置ui-icon-bullet(任意现有的样式),然后稍后进行更改。

该类定义了临时样式(如果您打算使用这个选项卡,以便您的页面的其余部分不受影响,最好为特定工具栏添加更多选择器)。

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

和下面的JavaScript:实际的图像将通过下面的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'); 
      }); 
    }); 
+1

这个版本适用于我,因为它允许我使用使用URL而不是CSS类定义的图像。 – Glenn 2011-10-26 02:12:14

+1

数据属性是HTML 5的一部分,但是这是[SO链接](http://stackoverflow.com/questions/5696464/are-html-data-attributes-safe-for-older-browsers-eg-ie- 6)显示,也适用于早期的浏览器。我刚刚发现它在内部使用jQuery,而jquery有一个data()方法。我们可以使用方法.data('img')而不是.attr('data-img') – msanjay 2011-10-28 05:44:24

0

我的解决方案,以自定义图标添加到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" } 
}); 
+0

你的答案是不可取的 – 2013-08-27 16:50:33

0

大厦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'); 
     } 
    }); 
} 
0
// 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 } }); 
0

在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" } 
}); 
+0

你的答案会从解释中受益。 – jpaugh 2016-07-23 02:22:46

相关问题