将 HTML 元素放置在所有元素的顶部

发布于 2025-01-11 09:03:01 字数 3067 浏览 0 评论 0原文

我正在尝试使用 CSS 开发一个工具提示,以与 Select2 一起使用。我使用 position:absolute;z-index: 99999; 作为工具提示。但工具提示并未出现在所有 HTML 元素之上。

$(document).ready(function() {
  $('.js-data-example-ajax').select2({
    placeholder: "select...",
    templateSelection: function(state) {
      if (!state.id) {
        return state.text;
      }      
      return $(`<span>${state.text}</span>`);
    },
    templateResult: function(state) {
      if (!state.id) {
        return state.text;
      }      
      return $("<span class='tooltip'>" +
            state.text +
            "<span class='tooltiptext'>" +
            state.value +
            "</span></span>");
    },
    ajax: {
      url: 'https://api.github.com/gists/public',
      dataType: 'json',
      processResults: function(data) {        
        var options = [];
          if (data) {
              $.each(data, function (index, text) {
                  options.push({
                    id: index,
                    text: text.owner.login,
                    value: text.commits_url,
                  });
              });
          }
          return {
              results: options,
              more: false,
          };
      },
      data: function (params) {
      return {
        q: params.term, // search term
        page: params.page
      };
    },
    }
  });
});
.js-data-example-ajax {
  width: 200px;
}

.top{
  color:red;
  border: 2px solid red;
  width: fit-content;
  z-index:100000;
}


.tooltip {
  position: relative;
  display: inline-block;
}

.tooltip .tooltiptext {
  visibility: hidden;
  min-width: 620px;
  background-color: #000000;
  color: #ffffff;
  border-radius: 6px;
  position: absolute;
  z-index: 99999;
  bottom: 125%;
  left: -30%;
  margin-left: -60px;
  opacity: 0;
  transition: opacity 0.3s;
}

.tooltip .tooltiptext::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: #fff transparent transparent transparent;
}

.tooltip:hover .tooltiptext {
  visibility: visible;
  opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet"/>



<select class="js-data-example-ajax" id="mySelect2" multiple>
</select>

这就是问题所在。

输入图片此处描述

您能帮我在所有 HTML 元素上方显示工具提示吗?

I am trying to develop a Tooltip using CSS to use with Select2. I used position: absolute; and z-index: 99999; for the Tooltip. But the Tooltip is not appearing above all HTML element.

$(document).ready(function() {
  $('.js-data-example-ajax').select2({
    placeholder: "select...",
    templateSelection: function(state) {
      if (!state.id) {
        return state.text;
      }      
      return $(`<span>${state.text}</span>`);
    },
    templateResult: function(state) {
      if (!state.id) {
        return state.text;
      }      
      return $("<span class='tooltip'>" +
            state.text +
            "<span class='tooltiptext'>" +
            state.value +
            "</span></span>");
    },
    ajax: {
      url: 'https://api.github.com/gists/public',
      dataType: 'json',
      processResults: function(data) {        
        var options = [];
          if (data) {
              $.each(data, function (index, text) {
                  options.push({
                    id: index,
                    text: text.owner.login,
                    value: text.commits_url,
                  });
              });
          }
          return {
              results: options,
              more: false,
          };
      },
      data: function (params) {
      return {
        q: params.term, // search term
        page: params.page
      };
    },
    }
  });
});
.js-data-example-ajax {
  width: 200px;
}

.top{
  color:red;
  border: 2px solid red;
  width: fit-content;
  z-index:100000;
}


.tooltip {
  position: relative;
  display: inline-block;
}

.tooltip .tooltiptext {
  visibility: hidden;
  min-width: 620px;
  background-color: #000000;
  color: #ffffff;
  border-radius: 6px;
  position: absolute;
  z-index: 99999;
  bottom: 125%;
  left: -30%;
  margin-left: -60px;
  opacity: 0;
  transition: opacity 0.3s;
}

.tooltip .tooltiptext::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: #fff transparent transparent transparent;
}

.tooltip:hover .tooltiptext {
  visibility: visible;
  opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet"/>



<select class="js-data-example-ajax" id="mySelect2" multiple>
</select>

Here is the issue.

enter image description here

Could you please help me to show the Tooltip above all the HTML elements ?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文