在滑入时滑动元素

时间:2016-06-30 07:03:06

标签: html css

所以我试图在影片上创建一个悬停,我的白框向右滑动,我的文字会滑回屏幕。

您可以从以下视频中看到,如果我将鼠标悬停在框的中间,但因为我使用的是负right属性,那么如果我将鼠标悬停在左侧,则会出现故障。有没有人知道我可以采取哪种方法让这项工作顺利进行?

Here is a video displaying what I am talking about

Here is a codepen

HTML

<div class="box one">
  <div class="navlink one">Home</div>
</div>
<div class="box two">
  <div class="navlink two">Pizza</div>
</div>
<div class="box three">
  <div class="navlink three">Plaything</div>
</div>

CSS

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  background-color: pink;
}

nav {
  position: fixed;
  top: 0;
  right: 0;
  border: 1px solid red;
  height: 200px;
  width: 120px;
}

nav a {
  background-color: #fff;
  float: right;
  padding: 10px;
  display: block;
  width: 100%;
  margin-bottom: 10px;
  transition: all .4s;
  text-decoration: none;
  color: black;
  font-weight: bold;
}

nav a:hover {
  color: yellow;
  margin-right: 10px;
  border-box: content-box;
  background-color: transparent;
}

.box,
.navlink {
  position: fixed;
  background-color: #fff;
  width: 110px;
  height: 35px;
  right: 0;
  transition: all .4s;
}

.navlink {
  right: -110px;
  font-size:24px;
  padding-top:5px;
  font-weight:bold;
  color:gold;
  background-color: transparent;
}

.box:hover {
  right:-110px;
}

.box:hover .navlink {
  right: 0;
}

.one {
  top: 0;
}

.two {
  top: 45px;
}

.three {
  top: 90px;
}

3 个答案:

答案 0 :(得分:1)

我要做的是在绝对位置的方框内的背景div。

这是我的提议:http://codepen.io/r3npi2/pen/JKNYmd

HTML:

<div class="box one">
  <div class="bg"></div>
  <div class="navlink one">Home</div>
</div>
<div class="box two">
  <div class="bg"></div>
  <div class="navlink two">Pizza</div>
</div>
<div class="box three">
  <div class="bg"></div>
  <div class="navlink three">Plaything</div>
</div>

CSS:

body {
  background-color: pink;
}

.box {
  position: fixed;
  width: 110px;
  height: 35px;
  right: 0;
}

.box .bg {
  position: absolute;
  background-color: #fff;
  width: 100%;
  height:100%;
  right: 0;
  top:0;
  transition: all .4s;
}

.navlink {
  position: absolute;
  right: -110px;
  font-size:24px;
  font-weight:bold;
  color:gold;
  background-color: transparent;
  width: 100%;
  height:100%;
  top:0;
  transition: all .4s;
  padding-top:5px;
  box-sizing: border-box;
}

.box:hover .bg {
  right:-110px;
}

.box:hover .navlink {
  right: 0;
}

.box.one {
  top: 0;
}

.box.two {
  top: 45px;
}

.box.three {
  top: 90px;
}

答案 1 :(得分:0)

也许这不是最好的方式,取决于您的最低浏览器要求,但它会这样做。

我建议使用3d变换来提高性能,因为它会触发gpu。

CSS:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  background-color: pink;
}

.fixed-wrap {
  position: fixed;
  right: 0;
}
.hover-box {
  margin-bottom: 10px;
  width: 110px;
  height: 35px;
  transition: all .4s;
  position: relative;
}
.hover-box::before {
    content: "";
    background-color: #fff;
    width: 110px;
    height: 35px;
    position: absolute;
    z-index: -2;
    transition: all .2s ease-in-out;
}
.navlink {
    font-size:24px;
    /* Height hack */
    line-height: 35px; 
    font-weight:bold;
    color:gold;
    transition: all .2s ease-in-out;
    transform: translate3d(110px,0,0);
}
.hover-box:hover .navlink {
    transform: translate3d(0,0,0);
}
.hover-box:hover:before {
    transform: translate3d(110px,0,0);
}

HTML

<div class="fixed-wrap">
    <div class="hover-box one">
        <div class="navlink one">Home</div>
    </div>
    <div class="hover-box two">
        <div class="navlink two">Home</div>
    </div>
    <div class="hover-box three">
        <div class="navlink three">Home</div>
    </div>
</div>

https://jsfiddle.net/xdc6umcd/

答案 2 :(得分:0)

建议将框对象保留为具有悬停状态的容器。但不是过渡

有2个子对象在框上转换:hover

.box:hover .slideOff{
left:100%;
}
.box:hover .slideOn{
left:0%;
}