首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义悬停效应位置问题

自定义悬停效应位置问题
EN

Stack Overflow用户
提问于 2016-11-03 16:02:41
回答 4查看 76关注 0票数 0

我试图定位一个按钮10 of从水平中心的屏幕。这是按钮代码。该按钮使用一些奇特的悬停效果,这是不移动的按钮。如何将10 of按钮从屏幕的水平中心移开?

代码语言:javascript
复制
body {
  background-color: green;
}
.button-2 {
  width: 140px;
  height: 50px;
  border: 2px solid white;
  float: left;
  text-align: center;
  cursor: pointer;
  position: relative;
  box-sizing: border-box;
  overflow: hidden;
  margin: 0 0 40px 50px;
}
.button-2 a {
  font-family: arial;
  font-size: 16px;
  color: white;
  text-decoration: none;
  line-height: 50px;
  transition: all .5s ease;
  z-index: 2;
  position: relative;
}
.eff-2 {
  width: 140px;
  height: 50px;
  top: -50px;
  background: white;
  position: absolute;
  transition: all .5s ease;
  z-index: 1;
}
.button-2:hover .eff-2 {
  top: 0;
}
.button-2:hover a {
  color: #666;
}
.watch-video-position {}
代码语言:javascript
复制
<div class="watch-video-position">
  <div class="button-2 watch-video-position">
    <div class="eff-2 watch-video-position"></div>
    <a href="#"> CLICK ME </a>
  </div>
</div>

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-11-03 16:07:19

你需要用这个:

代码语言:javascript
复制
.top {
  position: relative;
}
.top .button-2 {
  position: absolute;
  left: 50%;
  margin-left: -70px;
}

确保将唯一的类top添加到第一个<div>中,并使用它来定位。您还可以在.top上使用边距和填充。

代码语言:javascript
复制
body {
  background-color: green;
}
.button-2 {
  width: 140px;
  height: 50px;
  border: 2px solid white;
  float: left;
  text-align: center;
  cursor: pointer;
  position: relative;
  box-sizing: border-box;
  overflow: hidden;
  margin: 0 0 40px 50px;
}
.button-2 a {
  font-family: arial;
  font-size: 16px;
  color: white;
  text-decoration: none;
  line-height: 50px;
  transition: all .5s ease;
  z-index: 2;
  position: relative;
}
.eff-2 {
  width: 140px;
  height: 50px;
  top: -50px;
  background: white;
  position: absolute;
  transition: all .5s ease;
  z-index: 1;
}
.button-2:hover .eff-2 {
  top: 0;
}
.button-2:hover a {
  color: #666;
}
.top {
  position: relative;
  padding-top: 25px;
}
.top .button-2 {
  position: absolute;
  left: 50%;
  margin-left: -70px;
}
代码语言:javascript
复制
<div class="top watch-video-position">
  <div class="button-2 watch-video-position">
    <div class="eff-2 watch-video-position"></div>
    <a href="#"> CLICK ME </a>
  </div>
</div>

票数 1
EN

Stack Overflow用户

发布于 2016-11-03 18:26:04

代码语言:javascript
复制
I have made some changes in the css to class button-2.
.button-2 {
      float: none;
      margin: 0 auto;
      top: 10px;
    }

代码语言:javascript
复制
body {
      background-color: green;
    }
    .button-2 {
      border: 2px solid white;
      box-sizing: border-box;
      cursor: pointer;
      float: none;
      height: 50px;
      margin: 0 auto;
      overflow: hidden;
      position: relative;
      text-align: center;
      top: 10px;
      width: 140px;
    }
    .button-2 a {
      font-family: arial;
      font-size: 16px;
      color: white;
      text-decoration: none;
      line-height: 50px;
      transition: all .5s ease;
      z-index: 2;
      position: relative;
    }
    .eff-2 {
      width: 140px;
      height: 50px;
      top: -50px;
      background: white;
      position: absolute;
      transition: all .5s ease;
      z-index: 1;
    }
    .button-2:hover .eff-2 {
      top: 0;
    }
    .button-2:hover a {
      color: #666;
    }
    .watch-video-position {}
代码语言:javascript
复制
<div class="watch-video-position">
      <div class="button-2 watch-video-position">
        <div class="eff-2 watch-video-position"></div>
        <a href="#"> CLICK ME </a>
      </div>
    </div>

票数 2
EN

Stack Overflow用户

发布于 2016-11-03 16:08:46

尝试将它的父对象对为中心,并将其转移到它的父服务器中:

代码语言:javascript
复制
body {
  background-color: green;
  text-align: center;
}
.outer {
  width: 200px;
  margin: auto;
  background-color: blue;
}
.button-2 {
  width: 140px;
  height: 50px;
  border: 2px solid white;
  cursor: pointer;
  position: relative;
  box-sizing: border-box;
  overflow: hidden;
  margin: 0 0 40px 50px;
}
.button-2 a {
  font-family: arial;
  font-size: 16px;
  color: white;
  text-decoration: none;
  line-height: 50px;
  transition: all .5s ease;
  z-index: 2;
  position: relative;
}
.eff-2 {
  width: 140px;
  height: 50px;
  top: -50px;
  background: white;
  position: absolute;
  transition: all .5s ease;
  z-index: 1;
}
.button-2:hover .eff-2 {
  top: 0;
}
.button-2:hover a {
  color: #666;
}
.watch-video-position {}
代码语言:javascript
复制
<div class="watch-video-position outer">
  <div class="button-2 watch-video-position">
    <div class="eff-2 watch-video-position"></div>
    <a href="#"> CLICK ME </a>
  </div>
</div>

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

https://stackoverflow.com/questions/40406203

复制
相关文章

相似问题

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