防止元素缩小

时间:2019-01-14 20:20:30

标签: javascript css

我正在尝试在我的网站上编写此发现菜单部分的代码,一切都进行得很好,除了一件事情是,当我单击Js激活View more按钮但项目缩小时,我不知道为什么我试图将宽度设置为100%,但不起作用。我在这里搜索了堆栈溢出一点点,但没有发现任何帮助 这是我的代码:

.discover-ou-menu{
position: relative;

}
.section-heading{
  text-align: center;
  font-family: 'Roboto', sans-serif;
text-transform:capitalize;
margin: 0;
}
.section-heading::after{
  content: "";
  position: relative;
  left: 41rem;
  display: block;
  width: 50px;
  height: 5px;
  text-align: center;
  background-color: #ff9900;
  margin-bottom: 1.5rem;
  margin: 0;
}
/* .dom-container{
  position: relative;
} */
.nav-menu{
  text-align: center;
  padding-left: 16px;
  font-family: 'Roboto', sans-serif;
  font-weight: bold;
  text-transform: capitalize;
  font-size: 15px;
  margin-top: 5px;
  margin-bottom: 40px;
}
.nav-menu-item{
  position: relative;
  text-decoration: none;
  color: black;
  padding-right: 20px;
}
.menu{
  display: inline-block;
  font-family: 'Roboto', sans-serif;
}
.img-menu{
height: 200px;}
.menu-price{
font-size: 30px;
display: flex;
justify-content: flex-end;
}
.hide{
  display: none;
  transition: 5s ease;
}
#table{
  display: table;
}
.tr{
    display: table-row;
}
.td{
    display: table-cell;
  }
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title></title>
</head>
<link rel="stylesheet" href="style.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">

<body>
  <section class="discover-our-menu">
    <h1 class="section-heading">discover our menu</h1>
    <nav class="nav-menu">
      <a href="#" class="nav-menu-item">main</a>
      <a href="#" class="nav-menu-item">desserts</a>
      <a href="#" class="nav-menu-item">drinks</a>
    </nav>
    <div id="table">
      <div class="tr">
        <div class="td">
          <img src="images/discover our menu/menu1.png" class="img-menu" alt="">
          <div class="menu">
            <h2 class="menu-title">1. Ricotta and strawberry tiramisu</h2>
            <p class="menu-description">A silky tiramisu made with ricotta and fresh strawberries <br>egg | sugar | milk | mascarpone | coffee | brandy | cocoa | vanilla </p>
            <p class="menu-price">30.00$</p>
          </div>
          <!--td1 end-->
        </div>
        <div class="td">
          <img src="images/discover our menu/menu2.png" class="img-menu" alt="">
          <div class="menu">
            <h2 class="menu-title">2. Halloumi and camembert salad</h2>
            <p class="menu-description">A silky tiramisu made with ricotta and fresh strawberries <br>egg | sugar | milk | mascarpone | coffee | brandy | cocoa | vanilla </p>
            <p class="menu-price">30.00$</p>
          </div>

        </div>
        <!--td2 end-->

      </div>
      <!--End of row1-->
      <div class="tr">
        <div class="td">
          <img src="images/discover our menu/menu3.png" class="img-menu" alt="">
          <div class="menu">
            <h2 class="menu-title">3. Kelp and morel salad</h2>
            <p class="menu-description">A silky tiramisu made with ricotta and fresh strawberries <br>egg | sugar | milk | mascarpone | coffee | brandy | cocoa | vanilla </p>
            <p class="menu-price">30.00$</p>
          </div>
        </div>
        <div class="td">
          <img src="images/discover our menu/menu4.png" class="img-menu" alt="">
          <div class="menu">
            <h2 class="menu-title">4. Kelp and morel salad</h2>
            <p class="menu-description">A silky tiramisu made with ricotta and fresh strawberries <br>egg | sugar | milk | mascarpone | coffee | brandy | cocoa | vanilla </p>
            <p class="menu-price">30.00$</p>
          </div>
        </div>
      </div>
      <!--end row2-->
      <div class="hide">

      <div class="tr">
        <div class="td">
          <img src="images/discover our menu/menu1.png" class="img-menu" alt="">
          <div class="menu">
            <h2 class="menu-title">1. Ricotta and strawberry tiramisu</h2>
            <p class="menu-description">A silky tiramisu made with ricotta and fresh strawberries <br>egg | sugar | milk | mascarpone | coffee | brandy | cocoa | vanilla </p>
            <p class="menu-price">30.00$</p>
          </div>
        </div>
        <div class="td">
          <img src="images/discover our menu/menu2.png" class="img-menu" alt="">
          <div class="menu">
            <h2 class="menu-title">2. Halloumi and camembert salad</h2>
            <p class="menu-description">A silky tiramisu made with ricotta and fresh strawberries <br>egg | sugar | milk | mascarpone | coffee | brandy | cocoa | vanilla </p>
            <p class="menu-price">30.00$</p>
          </div>

        </div>

      </div>
      <!--End of row3-->
      <div class="tr">
        <div class="td">
          <img src="images/discover our menu/menu3.png" class="img-menu" alt="">
          <div class="menu">
            <h2 class="menu-title">3. Kelp and morel salad</h2>
            <p class="menu-description">A silky tiramisu made with ricotta and fresh strawberries <br>egg | sugar | milk | mascarpone | coffee | brandy | cocoa | vanilla </p>
            <p class="menu-price">30.00$</p>
          </div>
        </div>
        <div class="td">
          <img src="images/discover our menu/menu4.png" class="img-menu" alt="">
          <div class="menu">
            <h2 class="menu-title">4. Kelp and morel salad</h2>
            <p class="menu-description">A silky tiramisu made with ricotta and fresh strawberries <br>egg | sugar | milk | mascarpone | coffee | brandy | cocoa | vanilla </p>
            <p class="menu-price">30.00$</p>
          </div>
        </div>
      </div>
    </div>
<span class="view" onclick="show()">View more +</span>
</div>
  </section>
  <script>
    function show(){
      document.querySelector(".hide").style.display="block";
      document.querySelector(".hide").style.width="888px";
    }
  </script>
</body>

</html>

下面是我点击“查看更多”按钮的“ 之前”和“ 之后

的一些屏幕截图

之前 enter image description here

之后

enter image description here

2 个答案:

答案 0 :(得分:2)

只需在main元素和第n个子元素上使用一个类将其隐藏即可,而不是将它们包装起来。

document.querySelector("button").addEventListener("click", function () {
  document.querySelector('.table').classList.toggle('limited');
})
.table.limited .tr:nth-of-type(1n+4) {display: none;}
<div class="table limited">
  <div class="tr">1
  </div>
  <div class="tr">2
  </div>
  <div class="tr">3
  </div>
  <div class="tr">4
  </div>
  <div class="tr">5
  </div>
  <div class="tr">6
  </div>
</div>
<button class="more">more</button>

带有动画

document.querySelector("button").addEventListener("click", function() {
  document.querySelector('.table').classList.toggle('limited');
})
.table.limited .tr:nth-of-type(1n+4) {
  max-height: 0;
  overflow: hidden;
  opacity: 0;
}

.table .tr:nth-of-type(1n+4) {
  opacity: 1;
  transition: all 1s ease-in;
  max-height: 100em;
}
<div class="table limited">
  <div class="tr">1
  </div>
  <div class="tr">2
  </div>
  <div class="tr">3
  </div>
  <div class="tr">4
  </div>
  <div class="tr">5
  </div>
  <div class="tr">6
  </div>
</div>
<button class="more">more</button>

答案 1 :(得分:-1)

替换

.tr {
    display: table-row;
}

与此

.tr {
    display: table-row;
    float: left;
}

然后尝试