简体   繁体   English

如何使用没有按钮的jQuery UI图标?

[英]How to use jQuery UI icons without buttons?

jQuery UI comes with some good icons. jQuery UI带有一些好的图标。 How can I use them without buttons? 如何在没有按钮的情况下使用它们? Let's say how to create link with plus sign and to react on hover and click by changing icons? 让我们说如何创建带号的链接,并通过更改图标对悬停和点击做出反应? Here is the demo just with icon added. 是添加了图标的演示。

Upd 1: On hover icon should change the color from grey to black (please see it here ). 更新1:悬停图标应将颜色从灰色更改为黑色(请在此处查看 )。 In my demo it is black from the beginning. 在我的演示中,它从一开始就是黑色的。

Upd 2: Here is almost what I need - http://jsfiddle.net/and7ey/gZQzt/6/ - but without that background rectangle, I need just plus sign. 更新2:这几乎是我需要的 - http://jsfiddle.net/and7ey/gZQzt/6/ - 但没有那个背景矩形,我只需要加号。

Upd 3: Looks like it would be better not to use standard jQuery UI styles, but to refer directly to the images , but I don't know how use it. 更新3:看起来最好不要使用标准的jQuery UI样式,而是直接引用图像 ,但我不知道如何使用它。

Seems I need to define CSS like: 似乎我需要定义CSS,如:

width: 16px; 
height: 16px; 
background-image: url(images/ui-icons_222222_256x240.png);
background-position: -32px -128px;

Positions can be easily found at jquery.ui.theme.css file. 可以在jquery.ui.theme.css文件中轻松找到位置。 But how should I: 但是我应该怎么做:

  1. define correct background-image url? 定义正确的背景图像网址?
  2. modify CSS to react on click, hover? 修改CSS以对点击做出反应,悬停?

You can use the icons with any element by adding the ui-icon and the appropriate class for the icon you want to the element. 您可以通过为元素添加ui-icon和相应的图标,将图标与任何元素一起使用。

<a href="#" class="ui-icon ui-icon-plusthick"></a>

If you want to add text to the link as well as an icon you'll need to set the icon on a separate element from the text. 如果要向链接添加文本以及图标,则需要在文本的单独元素上设置图标。 For example: 例如:

<a href="#"><span class="ui-icon ui-icon-plusthick"></span><span>Text Here</span></a>

To change the icon on hover, you'll need to use some javascript. 要在悬停时更改图标,您需要使用一些javascript。 The following requires jQuery: 以下需要jQuery:

$(".MySelector").hover(function() { 
    $(this).removeClass("ui-icon-plusthick").addClass("ui-icon-minusthick");
}, function() { 
    $(this).removeClass("ui-icon-minusthick").addClass("ui-icon-plusthick");
});

This is not "good", but at least this works. 这不是“好”,但至少这是有效的。 Please specify your question further, what should happen on click and hover. 请进一步说明您的问题,点击和悬停时会发生什么。

Sample 样品

http://jsfiddle.net/gZQzt/2/ http://jsfiddle.net/gZQzt/2/

HTML HTML

<a href="#" class="img-link">
    <span class="ui-icon ui-icon-plusthick" style="display:inline-block;"></span>
    <span class="link-text">Link</span>
</a>

CSS CSS

.img-link {
    text-decoration: none;
}

.link-text {
    text-decoration: underline;
}

Try this: 尝试这个:

<a>
  <span class="ui-icon ui-icon-plusthick"></span>
</a>

and: 和:

$('a').hover(function(){$(this).toggleClass('ui-state-highlight')});

You can see it here: http://jsfiddle.net/gZQzt/4/ 你可以在这里看到它: http//jsfiddle.net/gZQzt/4/

Since I can't reply to answers yet, here's what I did using Kyle Traubermann's code: 由于我还不能回答答案,这就是我用Kyle Traubermann的代码做的事情:

<div class="ui-state-default" style="background: none; border: none;"><span class="ui-icon ui-icon-circle-tick"></span></div>

Basically, you have to put the state in a separate div. 基本上,你必须将状态放在一个单独的div中。 This changes the image to the right colour but it also adds a border and gloss to it so I had to disable that with nones. 这会将图像更改为正确的颜色,但它也会为其添加边框和光泽,因此我必须使用nones禁用它。 You might need a color: none; 您可能需要一种颜色:无; in there as well. 在那里。

There's probably a simpler way to do this but I don't really know much about CSS yet. 可能有一种更简单的方法可以做到这一点,但我还不太了解CSS。

UI icon as anchor (no text): UI图标作为锚点(无文字):

Works for me: 适合我:

<a href="http://wow.com"><span class="ui-state-default ui-corner-all ui-icon ui-icon-extlink"></span></a>

Relevant: order of ui class names. 相关:ui类名称的顺序。

Finally, I've decided just to use jQuery UI's image files - How to use one icon from different image files? 最后,我决定只使用jQuery UI的图像文件 - 如何使用不同图像文件中的一个图标? :

a:link,a:visited { /* for regular non-hovered, non-active link styles */
    width: 16px;  
    height: 16px;
    background-image:
    url(images/defaultStateImage.png); 
    background-position: -32px -128px;
}

a:hover { /* for hover/mouse-over styles */
    url(images/hoverStateImage.png); 
    background-position: -32px -128px;
}

a:active { /* for click/mouse-down state styles */
    url(images/clickStateImage.png); 
    background-position: -32px -128px;
}

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

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