將工具提示與相對父級對齊
P粉251903163
P粉251903163 2024-04-06 13:16:59
0
2
501

我花了兩天時間試圖解決這個問題,但我意識到我無法獨自完成。我試圖將我的工具提示相對於父級居中。這是我的程式碼=>

let collection = document.querySelectorAll("[data-text]");
collection.forEach((ele, ind) => {
    var element = document.createElement("p");
    element.className = "tooltip-text";
    element.innerText = ele.dataset.text;
    element.dataset.name = "_" + ele.id + ind;
    document.getElementById(ele.id).appendChild(element);
         
    document.querySelector('#' + ele.id).addEventListener('mouseover', () => {
        document.querySelector('[data-name="' + element.dataset.name + '"]').style.visibility = 'visible';
     }, false);

   document.querySelector('#' + ele.id).addEventListener('mouseleave', () => {
        document.querySelector('[data-name="' + element.dataset.name + '"]').style.visibility = 'hidden';
   }, false);

});
.container {
  display: flex;
  background: #ccc;
  justify-content: center;
  align-items: center;
  height: 95vh;
}

.c-content{
  width: 100%;
  max-width: 800px;
  position: relative;
  overflow: hidden;
  border-radius: 4px;
}

.toprightcontrols {
  margin: 0 1.2% 0 0;
  display: flex;
  position: absolute; 
  justify-content: flex-end;
  top: 0;
  right: 0;
  width: 150px;
  padding-top: 0;
  flex-wrap: nowrap;
}

.btnClass {
  padding: 10px;
  background: none;
  border: 0;
  outline: 0;
  cursor: pointer;
  align-self: center;
  justify-self: right;
}

.btnClass:before {
  content: url('https://img001.prntscr.com/file/img001/nLMdieVITRSq82yZdqlWOw.png');
}

p.tooltip-text {
  color: black;
  display: block;
  visibility: hidden;
  width: fit-content;
  position: absolute;
  border-radius: 2px;
  z-index: 1;
  background: white;
  pointer-events: none;
  padding: 6px 8px 20.2px 8px;
  font-size: 1rem;
  animation: fadein 0.2s ease-in;
  animation-fill-mode: forwards;
}

p.tooltip-text:before {
  content: "";
  position: absolute;
  bottom: 100%;
  left: 50%;
  margin-left: -8px;
  border: 8px solid transparent;
  border-bottom: 8px solid white;
}
<div class="container">
            <div id="c-content">
                <div class="toprightcontrols">
                    <span name="btn1" id="btn1" class="btnClass" data-text="Hello there"></span>
                    <span name="xxxxx" id="xxxxx" class="btnClass" data-text="Tooltip"></span>
                    <span name="something" id="something" class="btnClass" data-text="Click me"></span>
                    <span name="randomid" id="randomid" class="btnClass" data-text="I'm a text"></span>
               </div>
        </div>
</div>

預期的結果一定是這樣的 =>

你能幫我嗎?我嘗試使用 float、text-align、justify-content、margin: 0 auto,我看到的所有內容都集中但沒有任何效果

謝謝。

P粉251903163
P粉251903163

全部回覆(2)
P粉916760429

看來我找到了解決方案。我補充道:

.btnClass {
  display: flex;
  justify-content: center;
  align-items: center;
}

將所有內容置中,然後是:

p.tooltip-text {
  top:30px;
}

垂直偏移。

let collection = document.querySelectorAll("[data-text]");
  collection.forEach((ele, ind) => {
  var element = document.createElement("p");
  element.className = "tooltip-text";
  element.innerText = ele.dataset.text;
  element.dataset.name = "_" + ele.id + ind;
                  document.getElementById(ele.id).appendChild(element);
         
 document.querySelector('#' + ele.id).addEventListener('mouseover', () => {
                document.querySelector('[data-name="' + element.dataset.name + '"]').style.visibility = 'visible';
            }, false);

            document.querySelector('#' + ele.id).addEventListener('mouseleave', () => {
                document.querySelector('[data-name="' + element.dataset.name + '"]').style.visibility = 'hidden';
            }, false);

});
.container {
  display: flex;
  background: #ccc;
  justify-content: center;
  align-items: center;
  height: 95vh;
}

.c-content{
  width: 100%;
  max-width: 800px;
  position: relative;
  overflow: hidden;
  border-radius: 4px;
}

.toprightcontrols {
  margin: 0 1.2% 0 0;
  display: flex;
  position: absolute; 
  justify-content: flex-end;
  top: 0;
  right: 0;
  width: 150px;
  padding-top: 0;
  flex-wrap: nowrap;
}

.btnClass {
  padding: 10px;
  background: none;
  border: 0;
  outline: 0;
  cursor: pointer;
  align-self: center;
  justify-self: right;
  display: flex;
  justify-content: center;
  align-items: center;
}

.btnClass:before {
  content: url('https://img001.prntscr.com/file/img001/nLMdieVITRSq82yZdqlWOw.png');
}

p.tooltip-text {
  color: black;
  display: block;
  visibility: hidden;
  width: fit-content;
  position: absolute;
  top:30px;
  border-radius: 2px;
  z-index: 1;
  background: white;
  pointer-events: none;
  padding: 6px 8px 20.2px 8px;
  font-size: 1rem;
  animation: fadein 0.2s ease-in;
  animation-fill-mode: forwards;
}

p.tooltip-text:before {
  content: "";
  position: absolute;
  bottom: 100%;
  left: 50%;
  margin-left: -8px;
  border: 8px solid transparent;
  border-bottom: 8px solid white;
}
P粉578343994

將需要作為中心參考的元素設定為相對元素。在您的情況下,您希望與 .btnClass 保持一致。這樣,類別就可以包裝其內容。

.btnClass {
  position: relative;
}

之後您就可以將工具提示置中。將左側位置定義為50%,並使用transformtranslateX(-50%),這樣,transform 屬性就移動了元素本身的-50 %,這已經是向左移動了50%,所以結果是完美的對齊。

.tooltip-text {
  left: 50%;
  transform: translateX(-50%);
}

最終結果:

let collection = document.querySelectorAll("[data-text]");
collection.forEach((ele, ind) => {
var element = document.createElement("p");
element.className = "tooltip-text";
element.innerText = ele.dataset.text;
element.dataset.name = "_" + ele.id + ind;
document.getElementById(ele.id).appendChild(element);
         
    document.querySelector('#' + ele.id).addEventListener('mouseover', () => {
        document.querySelector('[data-name="' + element.dataset.name + '"]').style.visibility = 'visible';
     }, false);

   document.querySelector('#' + ele.id).addEventListener('mouseleave', () => {
        document.querySelector('[data-name="' + element.dataset.name + '"]').style.visibility = 'hidden';
   }, false);

});
.btnClass {
  position: relative;
}

.tooltip-text {
  left: 50%;
  transform: translateX(-50%);
}

.container {
  display: flex;
  background: #ccc;
  justify-content: center;
  align-items: center;
  height: 95vh;
}

.c-content{
  width: 100%;
  max-width: 800px;
  position: relative;
  overflow: hidden;
  border-radius: 4px;
}

.toprightcontrols {
  margin: 0 1.2% 0 0;
  display: flex;
  position: absolute; 
  justify-content: flex-end;
  top: 0;
  right: 0;
  width: 150px;
  padding-top: 0;
  flex-wrap: nowrap;
}

.btnClass {
  padding: 10px;
  background: none;
  border: 0;
  outline: 0;
  cursor: pointer;
  align-self: center;
  justify-self: right;
}

.btnClass:before {
  content: url('https://img001.prntscr.com/file/img001/nLMdieVITRSq82yZdqlWOw.png');
}

p.tooltip-text {
  color: black;
  display: block;
  visibility: hidden;
  width: fit-content;
  position: absolute;
  border-radius: 2px;
  z-index: 1;
  background: white;
  pointer-events: none;
  padding: 6px 8px 20.2px 8px;
  font-size: 1rem;
  animation: fadein 0.2s ease-in;
  animation-fill-mode: forwards;
}

p.tooltip-text:before {
  content: "";
  position: absolute;
  bottom: 100%;
  left: 50%;
  margin-left: -8px;
  border: 8px solid transparent;
  border-bottom: 8px solid white;
}
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板