简体   繁体   English

在悬停时显示外部div

[英]show external div on hover

i created a navigation and now i want to add a previewer to get related options. 我创建了一个导航,现在我想添加一个预览器来获取相关选项。

this div isnt connected to the navigation but it needs to show up on hover. 这个div没有连接到导航但它需要在悬停时显示。 i surrendered after "hours" of researching :/ 我在研究的“小时”后投降:/

Here is my simplified code 这是我的简化代码

https://codepen.io/solevita/pen/JXEOQg https://codepen.io/solevita/pen/JXEOQg

<div class="col">
  <div class="content">lorem ipsum</div>
</div>
<div class="col">
  <div class="navigation">
    <ul>
      <li><a href="#">punkt</a></li>
    </ul>
  </div>
</div>

as already told, the div I want to show is not in the navigation (for responsive reasons). 正如已经说过的那样,我要显示的div不在导航中(出于响应原因)。

Is it possible to handle this with pure css or is javascript necessary? 有可能用纯css处理这个或javascript是必要的吗? I hope this is not the case - my javascript skills are not existent :D 我希望事实并非如此 - 我的javascript技能不存在:D

thanks for your help! 谢谢你的帮助!

If you don't want to modify any existing markup, you'd likely need to do this with JavaScript. 如果您不想修改任何现有标记,则可能需要使用JavaScript执行此操作。 The '+' selector will only work with elements that are on the same level in the DOM. '+'选择器仅适用于DOM中同一级别的元素。 Since the element you're using to trigger the showing of the content div is not on the same level in the DOM as the content div itself, this method will not work. 由于您用于触发内容div显示的元素与DOM内容div本身不在同一级别,因此该方法不起作用。 If I am understanding this correctly, what you are trying to do can be accomplished with the following jQuery: 如果我正确理解了这一点,那么您尝试做的事情可以使用以下jQuery完成:

$('.navigation a').hover(function(e) {
  $('.content').show();
}, function(e) {
  $('.content').hide();
});

Here is a codepen with the Javascript (JQuery) version 这是一个带有Javascript(JQuery)版本的codepen

$(document).ready(
  function(){
    $("#punkt").mouseover(function() {
      $('.content').css('display','block');
    });
    $("#punkt").mouseout(function() {
      $('.content').css('display','none');
    });
});

https://codepen.io/anon/pen/rejdqq?editors=1111 https://codepen.io/anon/pen/rejdqq?editors=1111

If you move html of .content to be adjacent sibling of a element 如果你将.content html移动到a元素的相邻兄弟

 .content { display: none; } .navigation ul li a:hover + .content { display: block; } 
 <div class="col"> </div> <div class="col"> <div class="navigation"> <ul> <li><a href="#">punkt</a> <div class="content">lorem ipsum</div> </li> </ul> </div> </div> 

If you are sure about pure css, check this out: http://jsfiddle.net/raving/87865bq7/ 如果你确定纯css,请查看: http//jsfiddle.net/raving/87865bq7/

<!-- normally this stuff would be on the html element -->
<!--[if lt IE 7]>  <div class="ie ie6 lte9 lte8 lte7"> <![endif]-->
<!--[if IE 7]>     <div class="ie ie7 lte9 lte8 lte7"> <![endif]-->
<!--[if IE 8]>     <div class="ie ie8 lte9 lte8"> <![endif]-->
<!--[if IE 9]>     <div class="ie ie9 lte9"> <![endif]-->
<!--[if gt IE 9]>  <div> <![endif]-->
<!--[if !IE]><!--> <div>             <!--<![endif]-->
  <div class="wrapper">
    I have a tooltip.
    <div class="tooltip">I am a tooltip!</div>
  </div>
</div>

CSS: CSS:

.wrapper {
  text-transform: uppercase;
  background: #ececec;
  color: #555;
  cursor: help;
  font-family: "Gill Sans", Impact, sans-serif;
  font-size: 20px;
  margin: 100px 75px 10px 75px;
  padding: 15px 20px;
  position: relative;
  text-align: center;
  width: 200px;
  -webkit-transform: translateZ(0); /* webkit flicker fix */
  -webkit-font-smoothing: antialiased; /* webkit text rendering fix */
}

.wrapper .tooltip {
  background: #1496bb;
  bottom: 100%;
  color: #fff;
  display: block;
  left: -25px;
  margin-bottom: 15px;
  opacity: 0;
  padding: 20px;
  pointer-events: none;
  position: absolute;
  width: 100%;
  -webkit-transform: translateY(10px);
     -moz-transform: translateY(10px);
      -ms-transform: translateY(10px);
       -o-transform: translateY(10px);
          transform: translateY(10px);
  -webkit-transition: all .25s ease-out;
     -moz-transition: all .25s ease-out;
      -ms-transition: all .25s ease-out;
       -o-transition: all .25s ease-out;
          transition: all .25s ease-out;
  -webkit-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
     -moz-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
      -ms-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
       -o-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
          box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
}

/* This bridges the gap so you can mouse into the tooltip without it disappearing */
.wrapper .tooltip:before {
  bottom: -20px;
  content: " ";
  display: block;
  height: 20px;
  left: 0;
  position: absolute;
  width: 100%;
}  

/* CSS Triangles - see Trevor's post */
.wrapper .tooltip:after {
  border-left: solid transparent 10px;
  border-right: solid transparent 10px;
  border-top: solid #1496bb 10px;
  bottom: -10px;
  content: " ";
  height: 0;
  left: 50%;
  margin-left: -13px;
  position: absolute;
  width: 0;
}

.wrapper:hover .tooltip {
  opacity: 1;
  pointer-events: auto;
  -webkit-transform: translateY(0px);
     -moz-transform: translateY(0px);
      -ms-transform: translateY(0px);
       -o-transform: translateY(0px);
          transform: translateY(0px);
}

/* IE can just show/hide with no transition */
.lte8 .wrapper .tooltip {
  display: none;
}

.lte8 .wrapper:hover .tooltip {
  display: block;
}

Sadly (luckily) you cannot "navigate" using CSS up the DOM tree . 可悲的是 (幸运的是)你不能 在DOM树上使用CSS “导航”。 You can only target next siblings and child elements (as you probably already know). 您只能定位下一个兄弟姐妹和子元素(您可能已经知道)。

JavaScript can help you in this specific case: JavaScript可以在这种特定情况下帮助您:

 function showTarget(el) { var target = document.querySelector("[data-target='"+ el.dataset.hovershow +"']"); el.addEventListener("mouseenter", function() { target.classList.add("visible"); }); el.addEventListener("mouseleave", function() { target.classList.remove("visible"); }); } [].forEach.call(document.querySelectorAll("[data-hovershow]"), showTarget); 
 .navigation{ position:fixed; bottom: 30px; } .content { display: none; } .content.visible { display: block; } /* "visible" class is toggled by JS */ 
 <div class="col"> <div class="content" data-target="1">1 lorem ipsum</div> <div class="content" data-target="2">2 lorem ipsum</div> </div> <div class="col"> <div class="navigation"> <ul> <li><a href="#" data-hovershow="1">1 punkt</a></li> <li><a href="#" data-hovershow="2">2 punkt</a></li> </ul> </div> </div> 

If you move html of .content to be adjacent sibling of a element; 如果你将.content html移动到a元素的相邻兄弟; set position of .content to absolute , adjust left , top properties to match position of first .col element , you should be able to render .content within viewport similar to position of a child element of first .col element; .content position设置为absolute ,调整lefttop属性以匹配第一个.col元素的位置,您应该能够在视口中呈现类似于第一个.col元素的子元素位置的.content ; that is , above a element; 即,上述a元件; or at any position within viewport. 或在视口内的任何位置。 Given that .content display is set to none , it should not matter if .content is placed next to a element within html 鉴于.content display设置为none ,将.content放在html a元素旁边并不重要

 .col:nth-of-type(1) { top: 0px; left:0px; } .content { display: none; } .navigation ul li a:hover + .content { display: block; position: absolute; left:0px; top: 0px; } 
 <div class="col"> </div> <div class="col"> <div class="navigation"> <ul> <li><a href="#">punkt</a> <div class="content">lorem ipsum</div> </li> </ul> </div> </div> 

Another css approach would be to utilize ::before pseudo element, again setting position:absolute , adjusting top , left values to render text or url at specific position within viewport 另一种css方法是利用::before伪元素,再次设置position:absolute ,调整topleft值以在viewport中的特定位置渲染文本或url

 a:hover::before { content: "lorem ipsum\\A"; position: absolute; display:block; top:8px; left:60px; color:green; } 
 <div class="col"> <div class="content">content:</div> </div> <div class="col"> <div class="navigation"> <ul> <li><a href="#">punkt</a></li> </ul> </div> </div> 

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

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