jQueryMobile 自定义导航
我正在使用 jQuery Mobile 构建一个移动应用程序,我想在单个导航元素的左上角/右上角实现一个苹果风格的通知图标。
以下 URL 中的图像类似:
http://elephant.merryfull.com/images /mail_icon.jpg
我已经成功地使用以下 html/css/js
HTML
<header data-role="header" data-position="fixed">
<h1>Title</h1>
<nav data-role="navbar">
<ul>
<li><a href="a.html" class="ui-btn-active">link1</a></li>
<li><a href="b.html" data-icon="check" data-iconpos="top right">link2</a></li>
<li><a href="c.html">link3</a></li>
<li>
<div id="firstBadge" class="badges">
<a href="d.html">link4</a>
</div>
</li>
<li><a href="e.html">link5</a></li>
</ul>
</nav><!-- /navbar -->
</header><!-- /header -->
CSS
.badge
{
background-image: url(themes/base/images/notiwindow.png);
width: 16px;
height: 16px;
z-index: 20000;
}
Javascript
(function ($) {
$.fn.badge = function (action, options) {
// these are the default options
var defaults = {
top: '-8px',
left: '-8px',
cssClass: 'badge'
};
return this.each(function () {
var obj = $(this);
var eleId = this.id + "-badge";
// these are the 2 additional options
switch (action) {
case 'toggle':
$('#' + eleId).toggle();
return;
case 'hide':
$('#' + eleId).hide();
return;
}
// this merges the passed in settings with the default settings
var opts = $.extend({}, defaults, options);
if (!$("#" + eleId).length) {
var badge_html = "<div style='position:relative;float:left;'><div id='" + eleId + "' />8</div>";
obj.prepend(badge_html);
}
var badgeEle = $('#' + eleId);
badgeEle.addClass(opts.cssClass);
badgeEle.show().css({
position: 'absolute',
left: opts.left,
top: opts.top
});
return;
});
};
})(jQuery);
$(function () {
$('.badges').badge();
});
获得了一些基本内容问题不在于通知图标的位置,它应该按其应有的方式显示。问题是图像的顶部被父元素切断了。我尝试设置元素和父元素的索引以确保它始终位于顶部,但到目前为止还没有成功。代码的输出类似于以下 URL 中的图像:
http://elephant.merryfull.com /images/notification_icon.png
如果有人偶然发现了这个特定问题并有一个想要分享的解决方案,我们将不胜感激。
I'm building a mobile application using jQuery Mobile and I would like to implement an apple style notification icon in the top left/right corner of a single navigational element.
Something along the lines of the image in the following URL:
http://elephant.merryfull.com/images/mail_icon.jpg
I've managed to get something basic using the following html/css/js
HTML
<header data-role="header" data-position="fixed">
<h1>Title</h1>
<nav data-role="navbar">
<ul>
<li><a href="a.html" class="ui-btn-active">link1</a></li>
<li><a href="b.html" data-icon="check" data-iconpos="top right">link2</a></li>
<li><a href="c.html">link3</a></li>
<li>
<div id="firstBadge" class="badges">
<a href="d.html">link4</a>
</div>
</li>
<li><a href="e.html">link5</a></li>
</ul>
</nav><!-- /navbar -->
</header><!-- /header -->
CSS
.badge
{
background-image: url(themes/base/images/notiwindow.png);
width: 16px;
height: 16px;
z-index: 20000;
}
Javascript
(function ($) {
$.fn.badge = function (action, options) {
// these are the default options
var defaults = {
top: '-8px',
left: '-8px',
cssClass: 'badge'
};
return this.each(function () {
var obj = $(this);
var eleId = this.id + "-badge";
// these are the 2 additional options
switch (action) {
case 'toggle':
$('#' + eleId).toggle();
return;
case 'hide':
$('#' + eleId).hide();
return;
}
// this merges the passed in settings with the default settings
var opts = $.extend({}, defaults, options);
if (!$("#" + eleId).length) {
var badge_html = "<div style='position:relative;float:left;'><div id='" + eleId + "' />8</div>";
obj.prepend(badge_html);
}
var badgeEle = $('#' + eleId);
badgeEle.addClass(opts.cssClass);
badgeEle.show().css({
position: 'absolute',
left: opts.left,
top: opts.top
});
return;
});
};
})(jQuery);
$(function () {
$('.badges').badge();
});
The problem isn't with the positioning of the notification icon, it appears as it should. The problem is that the top of the image is cut off by the parent element. I've try setting the index of both the element and the parent to ensure that it always sits on top, but I've had no success as yet. The output of the code looks like the image in the following URL:
http://elephant.merryfull.com/images/notification_icon.png
If anyone has stumbled on this particular problem and has a solution that would like to share, it would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
单独设置
z-index
对于您的情况没有任何用处。您必须将徽章定位为absolute
,其父元素定位为relative
。或多或少像这样:nav > ul> li
asposition:relative
.badge
设置为position:absolute;顶部:0;左:0;背景:url(...)
(如有必要,请使用负的上边距和左边距)
徽章将覆盖在
li
元素上方,并且不会被切断。setting a
z-index
alone will not prove any useful in your case. You would have to position the badge asabsolute
with its parent element positionedrelative
. More or less like this:nav > ul > li
asposition:relative
.badge
asposition:absolute; top:0; left:0; background: url(...)
(use a negative top and left margin if necessary)
the badge will then be overlayed above the
li
element and will not be cut off.看看这里。
我在 JQM 类
.ui-li-count
上使用了 CSS,但您可以创建自己的元素并将其放置在您想要的任何位置。我还首先使用了背景图像,但这是额外的 HTTP 请求,因此我仅使用 CSS 完成了我的解决方案。
我没有尝试将其附加到普通的 JQM 导航栏,但它在我的修改版本加上其他一些 我把它拨到的地方。
这是CSS:
Have a look here .
I have used the CSS on the JQM class
.ui-li-count
but you could create your own element and place it whereever you want.I also used a background image first, but that's extra HTTP request, so I did my solution using CSS only.
I have not tried to append this to a normal JQM navbar, but it looks good on my modified version plus some other places I plucked it on.
Here is the CSS: