首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >显示固定位置元素中的所有内容

显示固定位置元素中的所有内容
EN

Stack Overflow用户
提问于 2018-11-15 07:09:05
回答 1查看 58关注 0票数 1

我在我的项目中有一个固定的元素,当我在移动设备中查看它时,由于视口比例的原因,元素中的文本被隐藏。当我的父元素具有fixed位置时,如何在响应式设计中显示我的内容?

文本位于我的代码的<div id="about-accordion">部分:

以下是我的代码:

代码语言:javascript
运行
复制
var accordions = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < accordions.length; i++) {
  accordions[i].addEventListener("click", function() {
    var patt = /(accordion-)(\d)/gm;
    var match = patt.exec(this.id);
    panel = document.getElementById("panel-" + match[2]);

    if (panel.style.maxHeight) {
      panel.style.maxHeight = null;
    } else {
      panels = document.getElementsByClassName('panel');
      for (j = 0; j < panels.length; j++) {
        if (panels[j].id != panel.id) {
          panels[j].style.maxHeight = null;
          if (accordions[j].id != this.id)
            accordions[j].classList.remove('active');
        }
      }
      panel.style.maxHeight = panel.scrollHeight + "px";
    }

    this.classList.toggle("active");
  });
}
代码语言:javascript
运行
复制
body {
  margin: 0;
  background-color: black;
}

.title {
  text-align: center;
  background-color: white;
  color: black;
  height: 100px;
  padding: 35px 0;
  margin: 0;
  width: 100%;
  z-index: 1;
  position: fixed;
  font-family: Clearface, sans-serif;
  /* Here you may change the name of the font family of the Header Title*/
  font-size: 40px;
  /* Here you may change the font size of the Header Title*/
}

.header #accordion-0 {
  font-size: 25px;
}

#about {
  background-color: black;
  color: white;
  float: left;
  width: 50%;
  height: 100vh;
  font-family: Clearface, sans-serif;
  margin-top: 170px;
  position: fixed;
}

#about h2 {
  font-size: 80px;
  /* Here you may change the font size of the About Title in desktop*/
  padding: 30px 70px 0 70px;
}

#about p {
  font-size: 30px;
  /* Here you may change the font size of the About Paragraph in desktop*/
  padding: 70px;
  padding-top: 0;
}

#projects .accordion {
  background-color: #333648;
  color: white;
  cursor: pointer;
  padding: 20px;
  width: 100%;
  border: none;
  text-align: center;
  outline: none;
  font-size: 17px;
  transition: 0.5s;
  height: 20%;
  font-family: 'Univers LT Std', sans-serif;
  /* Here you may change the name of the font family of Each Project Link*/
  font-size: 20px;
  /* Here you may change the font size of Each Project Link*/
  opacity: 0.7;
}

#projects .active,
#projects .accordion:hover {
  background-color: #ccc;
  color: black;
  opacity: 1;
}

.panel {
  padding: 0 18px;
  background-color: white;
  max-height: 0;
  overflow-y: hidden;
  transition: max-height 0.3s ease-out;
}

.post {
  padding: 0 20%;
}

.img-responsive {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 50px auto;
}

#projects {
  float: right;
  width: 50%;
  height: 100vh;
  font-family: 'Univers LT Std', sans-serif;
  /* Here you may change the name of the font family of Each Project*/
  font-size: 20px;
  /* Here you may change the font size of Each Project*/
  margin-top: 170px;
}

#about-accordion h2,
#about-accordion p {
  font-family: Clearface, sans-serif;
}

#about-accordion .panel {
  background-color: black;
  color: white;
}

#about-accordion {
  font-size: 30px;
  /* Here you may change the font size of About Section in mobile*/
}


/* Smartphones (portrait and landscape) ----------- */

@media only screen and (min-device-width: 320px) and (max-device-width: 480px) {
  .header #accordion-0 {
    padding-right: 1em;
  }
  #emma-back {
    padding-left: 1em;
  }
  #about {
    display: none;
  }
  #projects {
    width: 100%;
  }
  .title {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}


/* iPads (portrait and landscape) ----------- */

@media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {
  #about {
    display: none;
  }
  #projects {
    width: 100%;
  }
  .title {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .header #accordion-0 {
    padding-right: 1em;
  }
  #emma-back {
    padding-left: 1em;
  }
}


/* Desktops and laptops ----------- */

@media only screen and (min-width: 1224px) {
  #about-accordion {
    display: none;
  }
  .header #accordion-0 {
    display: none;
  }
  #dash {
    display: none;
  }
}


/* Large screens ----------- */

@media only screen and (min-width: 1824px) {
  #about-accordion {
    display: none;
  }
  .header #accordion-0 {
    display: none;
  }
  #dash {
    display: none;
  }
}
代码语言:javascript
运行
复制
<h1 class="header title">
  <span id="emma-back"> Hello <br> Back </span>
  <span id="dash"> - </span>
  <span id="accordion-0" class="accordion">About Me</span>
</h1>

<div id="about">
  <h2>
    Hello!
  </h2>

  <p>
    Lorem Ipsum är en utfyllnadstext från tryck- och förlagsindustrin. Lorem ipsum har varit standard ända sedan 1500-talet, när en okänd boksättare tog att antal bokstäver och blandade dem för att göra ett provexemplar av en bok. Lorem ipsum har inte bara
    överlevt fem århundraden, utan även övergången till elektronisk typografi utan större förändringar. Det blev allmänt känt på 1960-talet i samband med lanseringen av Letraset-ark med avsnitt av Lorem Ipsum, och senare med mjukvaror som Aldus PageMaker.
  </p>
</div>

<div id="projects">
  <div id="about-accordion">
    <div id="panel-0" class="panel">
      <div class="post">
        <h2>
          Hello!
        </h2>

        <p>
          Lorem Ipsum är en utfyllnadstext från tryck- och förlagsindustrin. Lorem ipsum har varit standard ända sedan 1500-talet, när en okänd boksättare tog att antal bokstäver och blandade dem för att göra ett provexemplar av en bok. Lorem ipsum har inte bara
          överlevt fem århundraden, utan även övergången till elektronisk typografi utan större förändringar. Det blev allmänt känt på 1960-talet i samband med lanseringen av Letraset-ark med avsnitt av Lorem Ipsum, och senare med mjukvaror som Aldus
          PageMaker.
        </p>
      </div>
    </div>
  </div>
  <button id="accordion-1" class="accordion">Project 1</button>
  <div id="panel-1" class="panel">
    <div class="post">
      <h2>TITLE HEADING</h2>
      <p>
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
        survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
        publishing software like Aldus PageMaker including versions of Lorem Ipsum.
      </p>
      <div>
        <img src="images/placeholder.jpg" class="img-responsive">
        <img src="images/placeholder.jpg" class="img-responsive">
        <img src="images/placeholder.jpg" class="img-responsive">
      </div>
    </div>
  </div>

  <button id="accordion-2" class="accordion">Project 2</button>
  <div id="panel-2" class="panel">
    <div class="post">
      <h2>TITLE HEADING</h2>
      <p>
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
        survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
        publishing software like Aldus PageMaker including versions of Lorem Ipsum.


      </p>
      <div>
        <img src="images/placeholder.jpg" class="img-responsive">
        <img src="images/placeholder.jpg" class="img-responsive">
        <img src="images/placeholder.jpg" class="img-responsive">
      </div>
    </div>
  </div>

  <button id="accordion-3" class="accordion">Project 3</button>
  <div id="panel-3" class="panel">
    <div class="post">
      <h2>TITLE HEADING</h2>
      <p>
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
        survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
        publishing software like Aldus PageMaker including versions of Lorem Ipsum.


      </p>
      <div>
        <img src="images/placeholder.jpg" class="img-responsive">
        <img src="images/placeholder.jpg" class="img-responsive">
        <img src="images/placeholder.jpg" class="img-responsive">
      </div>
    </div>
  </div>

  <button id="accordion-4" class="accordion">Project 4</button>
  <div id="panel-4" class="panel">
    <div class="post">
      <h2>TITLE HEADING</h2>
      <p>
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
        survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
        publishing software like Aldus PageMaker including versions of Lorem Ipsum.


      </p>
      <div>
        <img src="images/placeholder.jpg" class="img-responsive">
        <img src="images/placeholder.jpg" class="img-responsive">
        <img src="images/placeholder.jpg" class="img-responsive">
      </div>
    </div>
  </div>

  <button id="accordion-5" class="accordion">Project 5</button>
  <div id="panel-5" class="panel">
    <div class="post">
      <h2>TITLE HEADING</h2>
      <p>
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
        survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
        publishing software like Aldus PageMaker including versions of Lorem Ipsum.


      </p>
      <div>
        <img src="images/placeholder.jpg" class="img-responsive">
        <img src="images/placeholder.jpg" class="img-responsive">
        <img src="images/placeholder.jpg" class="img-responsive">
      </div>
    </div>
  </div>
</div>

在全页模式下运行代码片段时进行缩放,以了解我的意思

EN

回答 1

Stack Overflow用户

发布于 2018-11-15 09:13:28

正如您所提到的,因为您的固定元素(#about)是height: 100vh,所以文本被隐藏。

问题的另一半是,在该元素的上方是另一个元素(.header),要清除它,您需要设置margin-top: 170px

您需要在计算高度时考虑边距长度。

因此,不是这样:

代码语言:javascript
运行
复制
#about {
  height: 100vh;
  margin-top: 170px;
  position: fixed;
}

试试这个:

代码语言:javascript
运行
复制
#about {
  height: calc(100vh - 170px);
  overflow: auto;
  margin-top: 170px;
  position: fixed;
}

#projects元素也是如此。

jsFiddle demo

代码语言:javascript
运行
复制
var accordions = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < accordions.length; i++) {
  accordions[i].addEventListener("click", function() {
    var patt = /(accordion-)(\d)/gm;
    var match = patt.exec(this.id);
    panel = document.getElementById("panel-" + match[2]);

    if (panel.style.maxHeight) {
      panel.style.maxHeight = null;
    } else {
      panels = document.getElementsByClassName('panel');
      for (j = 0; j < panels.length; j++) {
        if (panels[j].id != panel.id) {
          panels[j].style.maxHeight = null;
          if (accordions[j].id != this.id)
            accordions[j].classList.remove('active');
        }
      }
      panel.style.maxHeight = panel.scrollHeight + "px";
    }

    this.classList.toggle("active");
  });
}
代码语言:javascript
运行
复制
body {
  margin: 0;
  background-color: black;
}

.title {
  text-align: center;
  background-color: white;
  color: black;
  height: 100px;
  padding: 35px 0;
  margin: 0;
  width: 100%;
  z-index: 1;
  position: fixed;
  font-family: Clearface, sans-serif;
  /* Here you may change the name of the font family of the Header Title*/
  font-size: 40px;
  /* Here you may change the font size of the Header Title*/
}

.header #accordion-0 {
  font-size: 25px;
}

#about {
  background-color: black;
  color: white;
  float: left;
  width: 50%;
  /* height: 100vh; */
  height: calc(100vh - 170px);  /* new */
  overflow: auto;               /* new */
  font-family: Clearface, sans-serif;
  margin-top: 170px;
  position: fixed;
}

#about h2 {
  font-size: 80px;
  /* Here you may change the font size of the About Title in desktop*/
  padding: 30px 70px 0 70px;
}

#about p {
  font-size: 30px;
  /* Here you may change the font size of the About Paragraph in desktop*/
  padding: 70px;
  padding-top: 0;
}

#projects .accordion {
  background-color: #333648;
  color: white;
  cursor: pointer;
  padding: 20px;
  width: 100%;
  border: none;
  text-align: center;
  outline: none;
  font-size: 17px;
  transition: 0.5s;
  height: 20%;
  font-family: 'Univers LT Std', sans-serif;
  /* Here you may change the name of the font family of Each Project Link*/
  font-size: 20px;
  /* Here you may change the font size of Each Project Link*/
  opacity: 0.7;
}

#projects .active,
#projects .accordion:hover {
  background-color: #ccc;
  color: black;
  opacity: 1;
}

.panel {
  padding: 0 18px;
  background-color: white;
  max-height: 0;
  overflow-y: hidden;
  transition: max-height 0.3s ease-out;
}

.post {
  padding: 0 20%;
}

.img-responsive {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 50px auto;
}

#projects {
  float: right;
  width: 50%;
  height: calc( 100vh - 170px); /* adjustment */
  overflow: auto;                /* new */
  font-family: 'Univers LT Std', sans-serif;
  /* Here you may change the name of the font family of Each Project*/
  font-size: 20px;
  /* Here you may change the font size of Each Project*/
  margin-top: 170px;
}

#about-accordion h2,
#about-accordion p {
  font-family: Clearface, sans-serif;
}

#about-accordion .panel {
  background-color: black;
  color: white;
}

#about-accordion {
  font-size: 30px;
  /* Here you may change the font size of About Section in mobile*/
}


/* Smartphones (portrait and landscape) ----------- */

@media only screen and (min-device-width: 320px) and (max-device-width: 480px) {
  .header #accordion-0 {
    padding-right: 1em;
  }
  #emma-back {
    padding-left: 1em;
  }
  #about {
    display: none;
  }
  #projects {
    width: 100%;
  }
  .title {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}


/* iPads (portrait and landscape) ----------- */

@media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {
  #about {
    display: none;
  }
  #projects {
    width: 100%;
  }
  .title {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .header #accordion-0 {
    padding-right: 1em;
  }
  #emma-back {
    padding-left: 1em;
  }
}


/* Desktops and laptops ----------- */

@media only screen and (min-width: 1224px) {
  #about-accordion {
    display: none;
  }
  .header #accordion-0 {
    display: none;
  }
  #dash {
    display: none;
  }
}


/* Large screens ----------- */

@media only screen and (min-width: 1824px) {
  #about-accordion {
    display: none;
  }
  .header #accordion-0 {
    display: none;
  }
  #dash {
    display: none;
  }
}
代码语言:javascript
运行
复制
<h1 class="header title">
  <span id="emma-back"> Hello <br> Back </span>
  <span id="dash"> - </span>
  <span id="accordion-0" class="accordion">About Me</span>
</h1>

<div id="about">
  <h2>
    Hello!
  </h2>

  <p>
    Lorem Ipsum är en utfyllnadstext från tryck- och förlagsindustrin. Lorem ipsum har varit standard ända sedan 1500-talet, när en okänd boksättare tog att antal bokstäver och blandade dem för att göra ett provexemplar av en bok. Lorem ipsum har inte bara
    överlevt fem århundraden, utan även övergången till elektronisk typografi utan större förändringar. Det blev allmänt känt på 1960-talet i samband med lanseringen av Letraset-ark med avsnitt av Lorem Ipsum, och senare med mjukvaror som Aldus PageMaker.
  </p>
</div>

<div id="projects">
  <div id="about-accordion">
    <div id="panel-0" class="panel">
      <div class="post">
        <h2>
          Hello!
        </h2>

        <p>
          Lorem Ipsum är en utfyllnadstext från tryck- och förlagsindustrin. Lorem ipsum har varit standard ända sedan 1500-talet, när en okänd boksättare tog att antal bokstäver och blandade dem för att göra ett provexemplar av en bok. Lorem ipsum har inte bara
          överlevt fem århundraden, utan även övergången till elektronisk typografi utan större förändringar. Det blev allmänt känt på 1960-talet i samband med lanseringen av Letraset-ark med avsnitt av Lorem Ipsum, och senare med mjukvaror som Aldus
          PageMaker.
        </p>
      </div>
    </div>
  </div>
  <button id="accordion-1" class="accordion">Project 1</button>
  <div id="panel-1" class="panel">
    <div class="post">
      <h2>TITLE HEADING</h2>
      <p>
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
        survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
        publishing software like Aldus PageMaker including versions of Lorem Ipsum.
      </p>
      <div>
        <img src="images/placeholder.jpg" class="img-responsive">
        <img src="images/placeholder.jpg" class="img-responsive">
        <img src="images/placeholder.jpg" class="img-responsive">
      </div>
    </div>
  </div>

  <button id="accordion-2" class="accordion">Project 2</button>
  <div id="panel-2" class="panel">
    <div class="post">
      <h2>TITLE HEADING</h2>
      <p>
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
        survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
        publishing software like Aldus PageMaker including versions of Lorem Ipsum.


      </p>
      <div>
        <img src="images/placeholder.jpg" class="img-responsive">
        <img src="images/placeholder.jpg" class="img-responsive">
        <img src="images/placeholder.jpg" class="img-responsive">
      </div>
    </div>
  </div>

  <button id="accordion-3" class="accordion">Project 3</button>
  <div id="panel-3" class="panel">
    <div class="post">
      <h2>TITLE HEADING</h2>
      <p>
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
        survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
        publishing software like Aldus PageMaker including versions of Lorem Ipsum.


      </p>
      <div>
        <img src="images/placeholder.jpg" class="img-responsive">
        <img src="images/placeholder.jpg" class="img-responsive">
        <img src="images/placeholder.jpg" class="img-responsive">
      </div>
    </div>
  </div>

  <button id="accordion-4" class="accordion">Project 4</button>
  <div id="panel-4" class="panel">
    <div class="post">
      <h2>TITLE HEADING</h2>
      <p>
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
        survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
        publishing software like Aldus PageMaker including versions of Lorem Ipsum.


      </p>
      <div>
        <img src="images/placeholder.jpg" class="img-responsive">
        <img src="images/placeholder.jpg" class="img-responsive">
        <img src="images/placeholder.jpg" class="img-responsive">
      </div>
    </div>
  </div>

  <button id="accordion-5" class="accordion">Project 5</button>
  <div id="panel-5" class="panel">
    <div class="post">
      <h2>TITLE HEADING</h2>
      <p>
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
        survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
        publishing software like Aldus PageMaker including versions of Lorem Ipsum.


      </p>
      <div>
        <img src="images/placeholder.jpg" class="img-responsive">
        <img src="images/placeholder.jpg" class="img-responsive">
        <img src="images/placeholder.jpg" class="img-responsive">
      </div>
    </div>
  </div>
</div>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53310107

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档