如何将网页划分为三个垂直部分?

时间:2018-01-04 11:52:05

标签: javascript html css

我想将目标网页划分为三个垂直部分。但我没有这样做。

这是我的代码:



const left = document.querySelector(".left");
const middle = document.querySelector(".middle");
const right = document.querySelector(".right");
const container = document.querySelector(".container");

left.addEventListener("mouseenter", () => {
  container.classList.add("hover-left");
});

left.addEventListener("mouseleave", () => {
  container.classList.remove("hover-left");
});

middle.addEventListener("mouseenter", () => {
  container.classList.add("hover-middle");
});

middle.addEventListener("mouseleave", () => {
  container.classList.remove("hover-middle");
});

right.addEventListener("mouseenter", () => {
  container.classList.add("hover-right");
});

right.addEventListener("mouseleave", () => {
  container.classList.remove("hover-right");
});

:root {
  --container-bg-color: #333;
  --left-bg-color: rgba(223, 39, 39, 0.7);
  --left-button-hover-color: rgba(161, 11, 11, 0.3);
  --middle-bg-color: rgba(39, 217, 223, 0.7);
  --middle-button-hover-color: rgba(14, 32, 196, 0.151);
  --right-bg-color: rgba(43, 43, 43, 0.8);
  --right-button-hover-color: rgba(92, 92, 92, 0.3);
  --hover-width: 70%;
  --other-width: 15%;
  --speed: 1000ms;
}

html,
body {
  padding: 0;
  margin: 0;
  font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
  width: 100%;
  height: 100%;
  overflow-x: hidden;
}

h1 {
  font-size: 3rem;
  color: #fff;
  position: absolute;
  left: 50%;
  top: 20%;
  transform: translateX(-50%);
  white-space: nowrap;
}

.button {
  display: block;
  position: absolute;
  left: 50%;
  top: 40%;
  height: 2.5rem;
  padding-top: 1.3rem;
  width: 15rem;
  text-align: center;
  color: #fff;
  border: #fff solid 0.2rem;
  font-size: 1rem;
  font-weight: bold;
  text-transform: uppercase;
  text-decoration: none;
  transform: translateX(-50%);
}

.split.left .button:hover {
  background-color: var(--left-button-hover-color);
  border-color: var(--left-button-hover-color);
}

.split.middle .button:hover {
  background-color: var(--middle-button-hover-color);
  border-color: var(--middle-button-hover-color);
}

.split.right .button:hover {
  background-color: var(--right-button-hover-color);
  border-color: var(--right-button-hover-color);
}

.container {
  position: relative;
  width: 100%;
  height: 100%;
  background: var(--container-bg-color);
}

.split {
  position: absolute;
  width: 33.33%;
  height: 100%;
  overflow: hidden;
}

.split.left {
  left: 0;
  background: url('https://image.ibb.co/m56Czw/designer.jpg') center center no-repeat;
  background-size: cover;
}

.split.left:before {
  position: absolute;
  content: "";
  width: 100%;
  height: 100%;
  background: var(--left-bg-color);
}

.split.middle {
  display: inline-block;
  background: url('https://image.ibb.co/m56Czw/designer.jpg') center center no-repeat;
  background-size: cover;
}

.split.middle:before {
  position: absolute;
  content: "";
  width: 100%;
  height: 100%;
  background: var(--middle-bg-color);
}

.split.right {
  right: 0;
  background: url('https://image.ibb.co/m3ZbRb/programmer.png') center center no-repeat;
  background-size: cover;
}

.split.right:before {
  position: absolute;
  content: "";
  width: 100%;
  height: 100%;
  background: var(--right-bg-color);
}

.split.left,
.split.middle,
.split.right,
.split.right:before,
.split.left:before,
.split.middle:before {
  transition: var(--speed) all ease-in-out;
}

.hover-left .left {
  width: var(--hover-width);
}

.hover-left .middle {
  width: var(--other-width);
}

.hover-left .right {
  width: var(--other-width);
}

.hover-left .middle:before {
  z-index: 2;
}

.hover-middle .middle {
  width: var(--hover-width);
}

.hover-middle .left {
  width: var(--other-width);
}

.hover-middle .right {
  width: var(--other-width);
}

.hover-middle .right:before {
  z-index: 2;
}

.hover-right .right {
  width: var(--hover-width);
}

.hover-right .middle {
  width: var(--other-width);
}

.hover-right .left {
  width: var(--other-width);
}

.hover-right .middle:before .left:before {
  z-index: 2;
}

@media(max-width: 800px) {
  h1 {
    font-size: 2rem;
  }
  .button {
    width: 12rem;
  }
}

@media(max-height: 700px) {
  .button {
    top: 70%;
  }
}

<body>
  <div class="container">
    <div class="split left">
      <h1>The Designer</h1>
      <a href="#" class="button">Read More</a>
    </div>

    <div class="split middle">
      <h1>The Middle</h1>
      <a href="#" class="button">Read More</a>
    </div>

    <div class="split right">
      <h1>The Programmer</h1>
      <a href="#" class="button">Read More</a>
    </div>
  </div>
  <script src="js/main.js"></script>
</body>
&#13;
&#13;
&#13;

我得到这样的输出: output of the current code

我想将 .middle 插入页面的中心部分?我哪里弄错了?过渡也相互重叠。

3 个答案:

答案 0 :(得分:0)

你可以尝试添加你的css:

..必须将中间容器向左移动33.3%(第一个容器结束的地方)

.split.middle {
 left: 33.3%
}

虽然最好的方法是使用display:flex https://css-tricks.com/snippets/css/a-guide-to-flexbox/

答案 1 :(得分:0)

您的CSS代码中有一些更改。你必须设置 middle 部分在悬停时的位置,如下所示

.split.middle {
  left: 33.333%;
}

.hover-left .middle {
  left: 70%;
}

.hover-middle .middle {
  left: 15%;
}

.hover-right .middle {
  left: 13%;
}

&#13;
&#13;
const left = document.querySelector(".left");
const middle = document.querySelector(".middle");
const right = document.querySelector(".right");
const container = document.querySelector(".container");

left.addEventListener("mouseenter", () => {
  container.classList.add("hover-left");
});

left.addEventListener("mouseleave", () => {
  container.classList.remove("hover-left");
});

middle.addEventListener("mouseenter", () => {
  container.classList.add("hover-middle");
});

middle.addEventListener("mouseleave", () => {
  container.classList.remove("hover-middle");
});

right.addEventListener("mouseenter", () => {
  container.classList.add("hover-right");
});

right.addEventListener("mouseleave", () => {
  container.classList.remove("hover-right");
});
&#13;
:root {
  --container-bg-color: #333;
  --left-bg-color: rgba(223, 39, 39, 0.7);
  --left-button-hover-color: rgba(161, 11, 11, 0.3);
  --middle-bg-color: rgba(39, 217, 223, 0.7);
  --middle-button-hover-color: rgba(14, 32, 196, 0.151);
  --right-bg-color: rgba(43, 43, 43, 0.8);
  --right-button-hover-color: rgba(92, 92, 92, 0.3);
  --hover-width: 70%;
  --other-width: 15%;
  --speed: 1000ms;
}

html,
body {
  padding: 0;
  margin: 0;
  font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
  width: 100%;
  height: 100%;
  overflow-x: hidden;
}

h1 {
  font-size: 3rem;
  color: #fff;
  position: absolute;
  left: 50%;
  top: 20%;
  transform: translateX(-50%);
  white-space: nowrap;
}

.button {
  display: block;
  position: absolute;
  left: 50%;
  top: 40%;
  height: 2.5rem;
  padding-top: 1.3rem;
  width: 15rem;
  text-align: center;
  color: #fff;
  border: #fff solid 0.2rem;
  font-size: 1rem;
  font-weight: bold;
  text-transform: uppercase;
  text-decoration: none;
  transform: translateX(-50%);
}

.split.left .button:hover {
  background-color: var(--left-button-hover-color);
  border-color: var(--left-button-hover-color);
}

.split.middle .button:hover {
  background-color: var(--middle-button-hover-color);
  border-color: var(--middle-button-hover-color);
}

.split.right .button:hover {
  background-color: var(--right-button-hover-color);
  border-color: var(--right-button-hover-color);
}

.container {
  position: relative;
  width: 100%;
  height: 100%;
  background: var(--container-bg-color);
}

.split {
  position: absolute;
  width: 33.33%;
  height: 100%;
  overflow: hidden;
}

.split.left {
  left: 0;
  background: url('https://image.ibb.co/m56Czw/designer.jpg') center center no-repeat;
  background-size: cover;
}

.split.left:before {
  position: absolute;
  content: "";
  width: 100%;
  height: 100%;
  background: var(--left-bg-color);
}

.split.middle {
  left: 33.333%;
  display: inline-block;
  background: url('https://image.ibb.co/m56Czw/designer.jpg') center center no-repeat;
  background-size: cover;
}

.split.middle:before {
  position: absolute;
  content: "";
  width: 100%;
  height: 100%;
  background: var(--middle-bg-color);
}

.split.right {
  right: 0;
  background: url('https://image.ibb.co/m3ZbRb/programmer.png') center center no-repeat;
  background-size: cover;
}

.split.right:before {
  position: absolute;
  content: "";
  width: 100%;
  height: 100%;
  background: var(--right-bg-color);
}

.split.left,
.split.middle,
.split.right,
.split.right:before,
.split.left:before,
.split.middle:before {
  transition: var(--speed) all ease-in-out;
}

.hover-left .left {
  width: var(--hover-width);
}

.hover-left .middle {
  width: var(--other-width);
  left: 70%;
}

.hover-left .right {
  width: var(--other-width);
}

.hover-left .middle:before {
  z-index: 2;
}

.hover-middle .middle {
  width: var(--hover-width);
  left: 15%;
}

.hover-middle .left {
  width: var(--other-width);
}

.hover-middle .right {
  width: var(--other-width);
}

.hover-middle .right:before {
  z-index: 2;
}

.hover-right .right {
  width: var(--hover-width);
}

.hover-right .middle {
  width: var(--other-width);
  left: 15%;
}

.hover-right .left {
  width: var(--other-width);
}

.hover-right .middle:before .left:before {
  z-index: 2;
}

@media(max-width: 800px) {
  h1 {
    font-size: 2rem;
  }
  .button {
    width: 12rem;
  }
}

@media(max-height: 700px) {
  .button {
    top: 70%;
  }
}
&#13;
<body>
  <div class="container">
    <div class="split left">
      <h1>The Designer</h1>
      <a href="#" class="button">Read More</a>
    </div>

    <div class="split middle">
      <h1>The Middle</h1>
      <a href="#" class="button">Read More</a>
    </div>

    <div class="split right">
      <h1>The Programmer</h1>
      <a href="#" class="button">Read More</a>
    </div>
  </div>
  <script src="js/main.js"></script>
</body>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以使用Flexbox在CSS中完全执行此操作。使容器<div>成为灵活容器:

.container {
  display: flex;
}

分割<div>的弹性项目:

.split {
  flex: 15 1 0;
}

15 (flex-grow):每个弹性项目将占用15&#34;份额&#34;额外的水平空间
1 (flex-shrink):flex项目将均匀收缩,每个项目放弃1&#34;分享&#34;水平空间 0 (flex-basis):每个弹性项目的基本宽度为0而不是基于其内容

为了扩大效果,让徘徊<div>贪婪:

.split:hover {
  flex-grow: 70;
}

您可以使用gradient backgrounds来避免使用extra :: before伪元素:

.split.middle {
  background:
    linear-gradient(var(--middle-bg-color), var(--middle-bg-color)),
    url('https://image.ibb.co/m56Czw/designer.jpg') center center no-repeat;
  background-size: cover;
}

这会创建一个背景图像,其渐变从一个半透明颜色渐变到相同的半透明颜色,并将其放在JPEG背景图像的顶部。

删除<div>上的所有定位代码,然后重新设置。

&#13;
&#13;
:root {
  --container-bg-color: #333;
  --left-bg-color: rgba(223, 39, 39, 0.7);
  --left-button-hover-color: rgba(161, 11, 11, 0.3);
  --middle-bg-color: rgba(39, 217, 223, 0.7);
  --middle-button-hover-color: rgba(14, 32, 196, 0.151);
  --right-bg-color: rgba(43, 43, 43, 0.8);
  --right-button-hover-color: rgba(92, 92, 92, 0.3);
  --hover-width: 70;
  --other-width: 15;
  --speed: 1000ms;
}

html,
body {
  padding: 0;
  margin: 0;
  font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
  width: 100%;
  height: 100%;
  overflow-x: hidden;
}

h1 {
  font-size: 3rem;
  color: #fff;
  position: absolute;
  left: 50%;
  top: 20%;
  transform: translateX(-50%);
  white-space: nowrap;
}

.button {
  display: block;
  position: absolute;
  left: 50%;
  top: 40%;
  height: 2.5rem;
  padding-top: 1.3rem;
  width: 15rem;
  text-align: center;
  color: #fff;
  border: #fff solid 0.2rem;
  font-size: 1rem;
  font-weight: bold;
  text-transform: uppercase;
  text-decoration: none;
  transform: translateX(-50%);
}

.split.left .button:hover {
  background-color: var(--left-button-hover-color);
  border-color: var(--left-button-hover-color);
}

.split.middle .button:hover {
  background-color: var(--middle-button-hover-color);
  border-color: var(--middle-button-hover-color);
}

.split.right .button:hover {
  background-color: var(--right-button-hover-color);
  border-color: var(--right-button-hover-color);
}

.container {
  position: relative;
  width: 100%;
  height: 100%;
  background: var(--container-bg-color);
  display: flex;
}

.split {
  position: relative;
  height: 100%;
  overflow: hidden;
  flex: var(--other-width) 1 0;
  transition: var(--speed) all ease-in-out;
}

.split.left {
  background:
    linear-gradient(var(--left-bg-color), var(--left-bg-color)),
    url('https://image.ibb.co/m56Czw/designer.jpg') center center no-repeat;
  background-size: cover;
}

.split.middle {
  background:
    linear-gradient(var(--middle-bg-color), var(--middle-bg-color)),
    url('https://image.ibb.co/m56Czw/designer.jpg') center center no-repeat;
  background-size: cover;
}

.split.right {
  background:
    linear-gradient(var(--right-bg-color), var(--right-bg-color)),
    url('https://image.ibb.co/m3ZbRb/programmer.png') center center no-repeat;
  background-size: cover;
}

.split:hover {
  flex-grow: var(--hover-width);
}

@media(max-width: 800px) {
  h1 {
    font-size: 2rem;
  }
  .button {
    width: 12rem;
  }
}

@media(max-height: 700px) {
  .button {
    top: 70%;
  }
}
&#13;
<body>
  <div class="container">
    <div class="split left">
      <h1>The Designer</h1>
      <a href="#" class="button">Read More</a>
    </div>

    <div class="split middle">
      <h1>The Middle</h1>
      <a href="#" class="button">Read More</a>
    </div>

    <div class="split right">
      <h1>The Programmer</h1>
      <a href="#" class="button">Read More</a>
    </div>
  </div>
</body>
&#13;
&#13;
&#13;