jQueryMobile 自定义导航

发布于 2024-12-12 06:55:48 字数 2942 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜尕妞 2024-12-19 06:55:48

单独设置 z-index 对于您的情况没有任何用处。您必须将徽章定位为absolute,其父元素定位为relative。或多或少像这样:

  • set nav > ul> li as position: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 as absolute with its parent element positioned relative. More or less like this:

  • set nav > ul > li as position:relative
  • set .badge as position: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.

千仐 2024-12-19 06:55:48

看看这里

我在 JQM 类 .ui-li-count 上使用了 CSS,但您可以创建自己的元素并将其放置在您想要的任何位置。

我还首先使用了背景图像,但这是额外的 HTTP 请求,因此我仅使用 CSS 完成了我的解决方案。

我没有尝试将其附加到普通的 JQM 导航栏,但它在我的修改版本加上其他一些 我把它拨到的地方

这是CSS:

.apple-navbar-ui li a .ui-li-count { 
   // setup
   color: #ffffff; 
   right: 0.5em; 
   font-size: 90%; 
   text-shadow: none; 
   font-weight: bold; 
   font-family: arial; 
   // shadow
   -moz-box-shadow: 0 1px 2px #999;  
   -webkit-box-shadow: 0 1px 2px #999; 
   box-shadow: 0 1px 2px #999; 
   padding-bottom: 1px;
   // border
   border: 0.15em solid #ffffff; 
   border-radius: 14px; 
   -moz-border-radius: 14px; 
   -webkit-border-radius: 14px; 
   // background
   background-color: #72b0d4; 
   line-height: 16px; 
   display: inline-block; 
   background-position: 0 0 !important; 
   // position CHANGE TO FIT YOUR NEED
   margin-right: 38%; 
   margin-top: -18px;
   // background gradient
   background-image: -webkit-gradient(linear,left top,left bottom, color-stop(0,#72b0d4),color-stop(1,#4b88b6)); 
   background-image: -webkit-linear-gradient(top, #72b0d4, #4b88b6);
   background-image: linear-gradient(top, #72b0d4, #4b88b6);
   background-image: -moz-linear-gradient(top, #72b0d4, #4b88b6);
   background-image: -o-linear-gradient(top, #72b0d4, #4b88b6);
   background-image: -ms-linear-gradient(top, #72b0d4, #4b88b6);
   filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='#72b0d4', EndColorStr='#4b88b6');
   -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorStr='#72b0d4', EndColorStr='#4b88b6')";
   }

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:

.apple-navbar-ui li a .ui-li-count { 
   // setup
   color: #ffffff; 
   right: 0.5em; 
   font-size: 90%; 
   text-shadow: none; 
   font-weight: bold; 
   font-family: arial; 
   // shadow
   -moz-box-shadow: 0 1px 2px #999;  
   -webkit-box-shadow: 0 1px 2px #999; 
   box-shadow: 0 1px 2px #999; 
   padding-bottom: 1px;
   // border
   border: 0.15em solid #ffffff; 
   border-radius: 14px; 
   -moz-border-radius: 14px; 
   -webkit-border-radius: 14px; 
   // background
   background-color: #72b0d4; 
   line-height: 16px; 
   display: inline-block; 
   background-position: 0 0 !important; 
   // position CHANGE TO FIT YOUR NEED
   margin-right: 38%; 
   margin-top: -18px;
   // background gradient
   background-image: -webkit-gradient(linear,left top,left bottom, color-stop(0,#72b0d4),color-stop(1,#4b88b6)); 
   background-image: -webkit-linear-gradient(top, #72b0d4, #4b88b6);
   background-image: linear-gradient(top, #72b0d4, #4b88b6);
   background-image: -moz-linear-gradient(top, #72b0d4, #4b88b6);
   background-image: -o-linear-gradient(top, #72b0d4, #4b88b6);
   background-image: -ms-linear-gradient(top, #72b0d4, #4b88b6);
   filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='#72b0d4', EndColorStr='#4b88b6');
   -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorStr='#72b0d4', EndColorStr='#4b88b6')";
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文