有没有一种简单的方法使用 JQuery 向 div 添加牙齿?

发布于 2024-10-12 06:47:42 字数 117 浏览 3 评论 0原文

基本的 div 是一个矩形。但有些网站(如 Facebook)顶部有一个小“牙齿”,指向图片或其他东西。

它给了它一颗向外的牙齿。

在 JQuery 中是否有一种简单的方法可以做到这一点?

A basic div is a rectangle. But some websites (like Facebook) have little "tooth" on the top of it, pointing to the picture or something.

It gives it a outward tooth.

Is there an easy easy easy way to do this in JQuery?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

以酷 2024-10-19 06:47:42

一种“简单”的方法是将绝对定位的图形添加到

中:(

$('div.someclass').each(function() {
    var $tooth = $('<img />', {
                src: ...,
            }).css({
                top: '-8px',
                position: 'absolute'
            });
    $(this).css('position', 'relative').append($tooth);
});

未经测试)

如果您的

有边框,则您可以可以让图形重叠并将边框“延伸”到箭头的尖端。


或者,我已经看到仅使用顶部带有“牙齿”的背景图像(下面丑陋的 ASCII 草图)即可实现此目的:

+-^----------+
|            |
|            |
+------------+

然后您只需要确保:

  1. 有将此图像作为背景图像(无重复、顶部、左侧),
  2. 背景图像始终大于
    (两个方向),并且
  3. 有一个额外的顶部填充(等于箭头的“大小”)。

One "easy" way would be to add an absolutely positioned piece of graphic to the <div>:

$('div.someclass').each(function() {
    var $tooth = $('<img />', {
                src: ...,
            }).css({
                top: '-8px',
                position: 'absolute'
            });
    $(this).css('position', 'relative').append($tooth);
});

(Untested)

If your <div> has a border, you could let the graphics overlap and "extend" the border to the tip of the arrow.


Alternatively, I've seen this achieved using only a background image with the "tooth" on the top (ugly ASCII sketch below):

+-^----------+
|            |
|            |
+------------+

Then you just need to make sure that:

  1. The <div> has this image as background image (no-repeat, top, left),
  2. The background image is always bigger than the <div> (both directions), and
  3. The <div> has an extra top-padding (equal to the "size" of the arrow).
假装不在乎 2024-10-19 06:47:42

不确定这是否是您正在寻找的...有一种纯 CSS 方法可以做到这一点,我经常这样做。除其他地方外,此处对此进行了描述。

要使牙齿朝上,请为牙齿添加一个 div:然后使用 0 像素矩形上的粗底部边框对其进行样式设置...

.tooth {
  width: 0; height: 0;
  border-color: transparent transparent black transparent;
  border-style: solid;
  border-width: 20px;
}

您需要将其绝对定位并调整位置它出现了。

jQuery 插件可能看起来像这样:

$.fn.addTooth = function(offset) {
    return $(this).each(function() {
      $('<div>').addClass('tooth').css({
         width: 0, height: 0,
         borderColor: 'transparent transparent black transparent',
         borderStyle: 'solid',
         borderWidth: '10px',
         position: 'absolute',
         top: -20,
         left: offset
      }).appendTo(this);
      $(this).css({position: 'relative'});
    });
  }

CSS 需要大量调整。

创建一个空心三角形有点困难,因为您需要两个矩形相互遮盖。我已经完成了这个(实际上还完成了更复杂的事情),但说实话,创建图形,然后添加它并以类似的方式定位它可能更容易。

Not sure if this what you're looking for... There's a pure CSS way to do this, which I do quite a bit. It's described here, among other places.

To make a tooth pointing up, add a div for the tooth: and then style it using a thick bottom border on a 0 pixel rectangle...

.tooth {
  width: 0; height: 0;
  border-color: transparent transparent black transparent;
  border-style: solid;
  border-width: 20px;
}

You'll need to position it absolutely and and adjust where it shows up.

As a jQuery plugin might look something like:

$.fn.addTooth = function(offset) {
    return $(this).each(function() {
      $('<div>').addClass('tooth').css({
         width: 0, height: 0,
         borderColor: 'transparent transparent black transparent',
         borderStyle: 'solid',
         borderWidth: '10px',
         position: 'absolute',
         top: -20,
         left: offset
      }).appendTo(this);
      $(this).css({position: 'relative'});
    });
  }

The CSS will need lots of tweaking.

Creating a hallowed out triangle is a little more difficult, as you need to two rectangles masking each other. I've done this (and more complex things, actually), but to be honest, it's probably just easier to create a graphic, and then add it and position it in a similar manner.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文