具有挑战性的CSS悬停

时间:2018-08-13 14:07:09

标签: javascript html css user-interface bootstrap-4

我对此实施感到困惑。

悬停时会出现黄色正方形/ bg,下图中应该有一个偏移量。我正在使用引导程序。任何帮助或朝着正确方向的观点将不胜感激。谢谢

This image is the design mockup and what i am trying to achieve.

3 个答案:

答案 0 :(得分:2)

此示例将使您能够进行动画悬停。它使用CSS转换:

.row {
  background: transparent;
  margin-top: 20px;
}

.col {
  border: solid 1px #6c757d;
  padding: 10px;
  position: relative;
}

.col-content::before {
  transition: all 0.25s ease-in;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  display: block;
  content: '';
  z-index: -1;
}

.col:hover .col-content::before {
  background: orange;
  transform: translateX(5px) translateY(5px);
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<!-- 
   Bootstrap docs: https://getbootstrap.com/docs
   -->
<div class="container">
   <div class="row">
      <div class="col">
         <div class="col-content">
            1 of 2
         </div>
      </div>
      <div class="col">
         <div class="col-content">
            2 of 2
         </div>
      </div>
   </div>
   <div class="row">
      <div class="col">
         <div class="col-content">
            1 of 3
         </div>
      </div>
      <div class="col">
         <div class="col-content">
            2 of 3
         </div>
      </div>
      <div class="col">
         <div class="col-content">
            3 of 3
         </div>
      </div>
   </div>
</div>

答案 1 :(得分:0)

这是一个非常基本的示例,但是应该给您一个想法。使用前伪元素(绝对位于z索引为-1的位置)和具有透明背景的容器应该可以为您提供所需的结果。参见示例。

body {
  margin: 0;
  box-sizing: border-box;
}
.parent {
  padding: 32px;
  background-color: #dfdbe5;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='40' height='40' viewBox='0 0 40 40'%3E%3Cg fill-rule='evenodd'%3E%3Cg fill='%239C92AC' fill-opacity='0.4'%3E%3Cpath d='M0 38.59l2.83-2.83 1.41 1.41L1.41 40H0v-1.41zM0 1.4l2.83 2.83 1.41-1.41L1.41 0H0v1.41zM38.59 40l-2.83-2.83 1.41-1.41L40 38.59V40h-1.41zM40 1.41l-2.83 2.83-1.41-1.41L38.59 0H40v1.41zM20 18.6l2.83-2.83 1.41 1.41L21.41 20l2.83 2.83-1.41 1.41L20 21.41l-2.83 2.83-1.41-1.41L18.59 20l-2.83-2.83 1.41-1.41L20 18.59z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
}

.row1,
.row2 {
  width: calc(100vw - 64px);
  position: relative;
  display: flex;
  background: #fff;
  clear: both;
  overflow: visible;
  margin-left: 5px;
}
.row1::after,
.row2::after {
  content: "";
  display: table;
  clear: both;
}
.row1 {
  z-index: 1;
}
.row2 {
  clear: left;
  z-index: 0;
}

span[class^="container"] {
  width: calc(100% - 64px / 3);
  float: left;
  position: relative;
  padding: 10px;
  border: 5px solid #6690ce;
  margin-left: -5px;
  margin-top: -5px;
}

span[class^="container"]:hover::before {
  content: "";
  background: #ffd100;
  position: absolute;
  padding: 5px;
  top: 3px;
  left: 3px;
  width: 100%;
  height: 100%;
  z-index: -1;
}
<section class="parent">
  <div class="row1">
    <span class="container1">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. </span>
    <span class="container2">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</span>
    <span class="container3">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. </span>
    <span class="container4">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. </span>
  </div>
  <div class="row2">
    <span class="container5">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. </span>
    <span class="container6">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. </span>
    <span class="container7">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</span>
  </div>
</div>

答案 2 :(得分:0)

这是一种实现具有偏移量的悬停效果的方法:

JSFiddle Example

如果您能够提供代码示例,我可以为您提供更多帮助。

<div class="container">
 <div class="heading">
  <h1>Hey</h1>
 </div>
</div>

.container{
  width: 100px;
  border: 1px solid red;
  text-align: center;
}

.heading:hover {
  width: 100px;
  border: 1px solid yellow;
  background-color: yellow;
  position: relative; 
  margin: 4px 0;
  left: 5px;
}