简体   繁体   English

如何将div元素放置在另一个位置? (一个将被隐藏并通过操作进行切换)

[英]how to place a div element in the same place as another? (one will be hidden and be toggled by action)

I want to have an image that when clicked will fade out and display text "behind it". 我想要一个图像,当单击该图像时该图像将淡出并在“其后”显示文本。 Right now all I can do is have my text appear above or below the image depending on where I move the element in the html file. 现在,我所能做的就是根据文本在html文件中的移动位置,使文本显示在图像的上方或下方。

If I can figure this out the plan is to have the text display:none and image display:block then have them toggled by an action of clicking on the image / clicking on the text div. 如果我能弄清楚,计划是使文本显示:无,图像显示:块,然后通过单击图像/单击文本div的动作来切换它们。

If this doesn't make sense I can try to clarify. 如果这没有意义,我可以尝试澄清。

I am working on my "tribute page" for free code camp. 我正在免费代码训练营的“致敬页面”上工作。 This is not a requirement but something extra I want to accomplish. 这不是必需条件,而是我想完成的其他事情。 The idea is to have my main image fade away and display the list items for "job history and Python timeline". 这个想法是让我的主图像淡出并显示“作业历史和Python时间轴”的列表项。 Everything is commented in my code pen link below. 下方的我的代码笔链接中注释了所有内容。 In question are the final two div elements "main image" and "job History and Python timeline" 问题是最后两个div元素“主图像”和“工作历史记录和Python时间轴”

Here is a link to my code pen 这是我的密码笔的链接

<!-- job history and Python timeline -->
<div id="history-timeline" class="text-center" style="display:show; inline">

  <p>
    <ul>
      <li>
        <li>
          <li>
            <li>
</div>

Add position:absolute to your image, so it will be out of the page flow and img will stack over h2 . 在图片中添加position:absolute ,因此它将不在页面流中,并且img将叠加在h2 now you can hide the image on click. 现在您可以单击隐藏图像。 I used display:none on hover of div. 我在div hover时使用了display:none make sure your parent div height match the height of image. 确保您的父div高度与图片的高度匹配。

 .hide_on_hover{ width:200px; height:200px; } .hide_on_hover img{ position:absolute; } .hide_on_hover:hover img{ display:none; } 
 <div id="main-image-div" class="text-center hide_on_hover"> <img id="main-image" src="http://sdtimes.com/wp-content/uploads/2014/08/0815.sdt-python.jpg" class="rounded img-thumbnail " width="200" height="200"> <!-- job history and Python timeline --> <h2 id="history-timeline">does this work</h2> </div> 

This can easily be accomplished by adjusting the positioning and z-indexing. 这可以通过调整定位和z-indexing轻松实现。

Your parent div element will want to have a position: relative attribute. 您的父div元素将要具有position: relative属性。 The text will want to then have a position: absolute attribute. 文本将需要具有position: absolute属性。 This will allow the text to overlap your image. 这将使文本与您的图像重叠。 Position relative tells the child element to position themselves respective to the closes parent with a relative attribute. 位置相对告诉子元素将自身定位为具有相对属性的关闭父对象。

The next step is to apply a few more attributes to control the layer. 下一步是应用更多属性来控制图层。 From my understanding, you'll want the image to be the top layer, and the text be the lower layer. 根据我的理解,您将希望图像是顶层,文本是底层。 Solution for this is to apply position: relative; z-index: 2 解决的办法是申请position: relative; z-index: 2 position: relative; z-index: 2 to your image and add z-index: 1 to your text. position: relative; z-index: 2到您的图像,然后将z-index: 1添加到您的文本。 The z-index attribute gives a layer order to the overlapping elements. z-index属性为重叠的元素赋予层顺序。 We need to give position: relative to the image because the default position of that element ignore z-index attributes. 我们需要指定position: relative对于图片的位置,因为该元素的默认位置会忽略z-index属性。 Things should start looking closer now. 现在事情应该开始变得更近了。

Now, you mentioned using a display: block and display: none attribute to hide your image and show the text. 现在,您提到使用display: blockdisplay: none属性来隐藏图像并显示文本。 This would work based on the functionality you described, but will create a jump effect since setting to display: none loses height settings so you'll get things collapsing on your page. 这将根据您描述的功能而起作用,但是自设置为显示后会产生跳跃效果:没有一个会丢失高度设置,因此您的页面会塌陷。 If you dont want this, you'll want to use visibility: hidden and visibility: visible instead. 如果您不希望这样做,则将改为使用“ visibility: hidden和“ visibility: visible

However, what I would probably do instead is add a new css class: 但是,我可能要做的是添加一个新的CSS类:

.hidden{ opacity: 0 }

This way you can have simpler javascript and also can go to the extent of animating the fade effect. 这样,您可以使用更简单的javascript,还可以达到淡入淡出效果的程度。 Speaking of Javascript, this would use the following code: 说到Javascript,它将使用以下代码:

$("#main-image").click(function(){ $(this).toggleClass('hidden') });

Here's what some your modified code looks like: 修改后的代码如下所示:

HTML: HTML:

<div id="main-image-div" class="text-center" style="position: relative">
<img id="main-image" src="http://sdtimes.com/wp-content/uploads/2014/08/0815.sdt-python.jpg" style="margin-top:2%; position: relative; z-index: 2;" class="rounded img-thumbnail">
<h2 id="history-timeline" style="position: absolute; z-index: 1; ">does this work</h2>        
</div>

CSS CSS

.hidden{opacity: 0}

Outside the scope of this, you should avoid doing inline styling and have the html just use classes. 在此范围之外,您应该避免进行内联样式设置,而让html只使用类。 Reference those classes in your css code and apply your styling there. 在CSS代码中引用这些类,然后在其中应用样式。 This allows for cleaner, more manageable code as your site gets built up. 这可以在您的网站建立时提供更简洁,更易管理的代码。

Try this code 试试这个代码

   $(document).ready(function(){

      $("#main-image").click(function(){
         $(this).removeClass('show-block');
        $(this).addClass('hide-block');
        $('#history-timeline').removeClass("hide-block");
        $('#history-timeline').addClass("show-block");

      });
        $("#history-timeline").click(function(){
          $(this).removeClass('show-block');
        $(this).addClass('hide-block');
        $('#main-image').removeClass("hide-block");
          $('#main-image').addClass("show-block");

      });

    });

DEMO DEMO

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

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