简体   繁体   English

在html链接上放置一个彩色圆圈

[英]Placing a colored circle over html link

Is there a simple (using css/js/jquery) way to place a transparent colored circle centered on top of a link, as shown in the example below? 是否有一种简单的方法(使用css / js / jquery)在链接顶部居中放置透明的彩色圆圈,如下例所示? The text in the gray box would also be nice. 灰色框中的文字也很好。

在此处输入图片说明

I tried by starting with the answer to this question: Put background image over text? 我首先从以下问题的答案开始进行尝试: 将背景图片放在文字上? . But when I shortened the colored overlay to less than 100%, I couldn't get it to center over the text (let alone appear as a circle). 但是,当我将彩色叠加层缩短到少于100%时,我无法使其位于文本的中心(更不用说显示为圆圈了)。

Sorry but this isn't my strongest area and I don't know if what I'm trying to do is stupid simple or way too hard. 抱歉,但这不是我最擅长的领域,我不知道我想做的事是愚蠢的简单还是太难了。 Thanks for any help. 谢谢你的帮助。

UPDATE: Reading some proposed solutions I realize I should have been much clearer in my original question. 更新:阅读一些建议的解决方案后,我意识到我在最初的问题中应该更加清楚。

The first important point I left out is that I'm not looking for a hover effect; 我忽略的第一个重要点是,我并不是在寻找悬停效果。 when the user calls up the page, I would like all the circles (and text, if possible) to be showing. 当用户调出页面时,我希望显示所有圆圈(如果可能,还显示文字)。

The second important point is that my goal is to add the styling dynamically, so I won't know in advance where the links will be or how big they will be. 第二个重要点是,我的目标是动态添加样式,因此我不会事先知道链接在哪里或链接的大小。 The information I'll know will be the link itself (the URL and ID) and the color of the circle needed for that link. 我知道的信息将是链接本身(URL和ID)以及该链接所需的圆圈的颜色。 (What I'm trying to do is create a heat map indicating which links on a page got the most clicks.) (我想做的是创建一个热图,指示页面上的哪些链接获得了最多的点击。)

Which brings me to the third point I left out: not all the circles will be the same color. 这使我想到了我遗漏的第三点:并不是所有的圆圈都具有相同的颜色。 I'm looking for 4 color options; 我正在寻找4种颜色选择; a page might have three green circles and two purple circles, for example. 例如,一个页面可能有三个绿色圆圈和两个紫色圆圈。

I'm sorry my incomplete question sent folks down the wrong path trying to help me. 抱歉,我的不完整问题使人们误入歧途,试图为我提供帮助。

There are multiple ways to do this, I used margin: auto with top: 0; left: 0; bottom: 0; right:0; 有多种方法可以做到这一点,我使用margin: auto with top: 0; left: 0; bottom: 0; right:0; top: 0; left: 0; bottom: 0; right:0; for the first and translate for the second. 第一次translate ,第二次translate

 .container { position: relative; width: fit-content; width: -moz-fit-content; } .circle { background: #909; width: 4em; height: 4em; border-radius: 2em; opacity: 0.4; } #circle-1 { position: absolute; margin: auto; top: 0; left: 0; bottom: 0; right: 0; } #circle-2 { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } .grey-box { background: #999; font-size: 0.8em; border: 1px solid #222; padding: 0.2em; position: absolute; top: -70%; left: 50%; } 
 <div class="container"> <h2>Getting a head start in the health sector</h2> <div id="circle-1" class="circle"></div> <div class="grey-box">Total: 47(0.1%) Unique: 28(0.1%)</div> </div> Established health sector companies can meet patient needs and cut their own costs by collaborating with startups, research shows. <div class="container"> <h3>Judge Business School</h3> <div id="circle-2" class="circle"></div> </div> 

You can achieve the circle without modifying the HTML at all, using a CSS-only solution: 您可以使用仅CSS的解决方案来实现循环,而无需完全修改HTML:

 a { position: relative; } a::after { content: ""; display: block; position: absolute; pointer-events: none; top: 0; left: 0; bottom: 0; right: 0; margin: auto; height: 2em; width: 2em; background-color: blue; opacity: 0; transition: opacity 0.2s; border-radius: 50%; } a:hover::after { opacity: .5; } 
 <a href>Getting a head start in the health sector</a> 

It only requires that the <a> tags be position: relative , which usually doesn't cause any visible differences in existing styling, since links by default are display: inline . 它仅要求<a>标记为position: relative通常不会在现有样式中造成任何可见的差异,因为默认情况下,链接为display: inline

In order to get the tooltip working, you can by utilizing both ::before and ::after , and dynamically populating the tooltip's text by employing the use of data- attributes, again to avoid as many changes to the existing HTML as possible: 为了使工具提示起作用,您可以同时使用::before::after ,并通过使用data- attribute属性动态填充工具提示的文本,再次避免对现有HTML进行尽可能多的更改:

 document.querySelectorAll('a').forEach(a => { a.setAttribute('data-tooltip', 'Total: 47 (0.1%) Unique: 28 (0.1%)'); }); 
 body { padding: 3em; } a { position: relative; } a::before { content: ""; display: block; position: absolute; pointer-events: none; top: 0; left: 0; bottom: 0; right: 0; margin: auto; height: 2em; width: 2em; background-color: blue; opacity: 0; transition: opacity 0.2s; border-radius: 50%; } a::after { content: attr(data-tooltip); display: inline-block; position: absolute; pointer-events: none; bottom: 100%; left: 50%; white-space: nowrap; padding: 0.25em; font-size: 0.6em; color: black; background-color: lightgrey; border: 1px solid black; opacity: 0; transition: opacity 0.2s; } a:hover::before { opacity: .5; } a:hover::after { opacity: 1; } 
 <a href>Getting a head start in the health sector</a> 

Maybe can try to convert tag to an inline-block, and with ::after pseudo-element draw the circle using :hover pseudo-class. 也许可以尝试将标记转换为行内块,然后使用:: after伪元素使用:hover伪类绘制圆圈。

Example: 例:

 .circle-link { display: flex; justify-content: center; position: relative; } .circle-link span { display: block; height: 20px; width: 20px; position: absolute; visibility: hidden; background-color: blue; opacity: .5; border-radius: 50%; } .circle-link:hover span { visibility: visible; } 
 <div class="circle-link"> <a href="my_link.com" >Here is the text of my link</a> <span></span> </div> 

You should'nt need to use Javascript or Jquery for this unless your using an event listener function ie. 除非您使用事件监听器功能,否则您不需要为此使用Javascript或Jquery。 for mouse over, click or hover effect, but even these can be achieved using pure Css, use the following attributes/pseudo-classes in your CSS: 对于鼠标悬停,单击或悬停效果,但即使使用纯Css也可以实现,请在CSS中使用以下属性/伪类:

    ::before & ::after
    & or, :hover & :target   

,that one of the other posters has mentioned to "capture" events. ,其他海报中的一位提到要“捕捉”事件。
To get a circle, just give a div the CSS attribute of; 要获得一个圆,只需给div的CSS属性即可;

     border-radius:50%;
     bakground-color:You Choose purple, blue or hex value;

& the width:; & 宽度:; You choose, will determine the size of the circle. 您选择,将确定圆的大小。 & set them with; 并设置它们;

      position:absolute;
      top:?;   right:?;   bottom:?;  left:?;  /*To move them 
                                                   around

If I remember correctly, left:Moves it right, right:moves it left. 如果我没记错的话,left:向右移动,right:向左移动。
If you use the z-index property on both the links, and the circle divs you would not have to have the circles transparent, but it is totally dependant on how you wish to style it. 如果在链接和圆div上同时使用z-index属性,则不必使圆透明,但它完全取决于您希望为其设置样式的方式。 Currently in the example above the circles are overlaping the text, & distorting the color of the link text which may not be desirable & can detract from the overall look. 当前,在上面的示例中,圆圈使文本重叠,并且扭曲了链接文本的颜色,这可能是不希望的,并且会损害整体外观。 Not only this but it takes away the contrast of the text element. 不仅如此,它还消除了文本元素的对比度。 You want the text to be bold, or brighter than elements behind it. 您希望文本加粗或比其后面的元素亮。

with the tool tips, I would give them a border radius of anywhere from 2/5px and maybe animate a glow beneath them.. Goodluck, Some of the other answers above are good aswell.. 借助工具提示,我可以为它们提供2 / 5px范围内的边框半径,并且可以在它们下面设置发光效果。.Goodluck,上面的其他一些回答也很好。

The Greyish tool tips you have mentioned can be made by creating two divs, around 18px height, 128px width. 您可以通过创建两个div来制作您提到的Greyish工具提示,这些div的高度约为18px,宽度约为128px。 Space them out using position element then make two smaller divs, (maybe 9px to 14px)both with the same background color as the initial two you made above, position them using position absolute. 使用position元素将它们隔开,然后制作两个较小的div(也许9px至14px),它们的背景颜色都应与您在上面创建的两个div相同,并使用absolute位置对其进行定位。 So you have large grouped with a small in two pairs. 因此,您将大的分为两小对。 use Transform:Rotate(45degs) on the two smaller ones. 对两个较小的对象使用Transform:Rotate(45degs)。 & possibly Transform:Skew(?) to achieve desired angle. 甚至可能使用Transform:Skew(?)获得所需的角度。 Then set their Zindex to be behind the larger ones. 然后将其Zindex设置在较大的Zindex之后。

Might be fiddley, getting the position right & theres probably a much easier method. 可能会很无聊,找到正确的位置,这可能是一种简单得多的方法。 But i'm still learning as I go... 但是我仍在学习中...

Goodluck. 祝好运。

Just create and div with border-radius 50% with some background and size, create and div width tooltip text. 只需使用具有某些背景和大小的50%边框半径创建和div,然后创建和div宽度工具提示文本即可。 Set position of elements to absolute. 将元素的位置设置为绝对。 Add an event listener to mouse move, and move your elements to cursor position when it over some link. 将事件侦听器添加到鼠标移动,并将元素移到某个链接上时将其移动到光标位置。 It will look very nice, especialy if you add some animation to moving. 看起来特别好,特别是如果您要添加一些动画来移动的话。

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

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