悬停在一个元素上会影响所有元素

时间:2017-01-03 13:09:45

标签: css3 css-transitions

我想在左侧创建四个元素,其中文本不在视图中。只悬停在一个元素上,它应该滑入,但所有元素都会滑入。 我此时并不知道。为什么所有元素都会滑入?

<div class="fixed-icons">
<div class="fixed-icons__item fixed-icons__item--booking">
    <span>booking</span>
</div>
<div class="fixed-icons__item fixed-icons__item--voucher">
    <span>voucher</span>
</div>
<div class="fixed-icons__item fixed-icons__item--phone">
    <span>phone</span>
</div>
<div class="fixed-icons__item fixed-icons__item--eye">
    <span>eye</span>
</div>
</div>

SCSS

.fixed-icons {
position: fixed;
top:40%;
right:0;

.fixed-icons__item {
border: 1px solid white;
background-color: #004576;
color: white;
padding: 0.25rem;
margin: 0.25rem;
cursor: pointer;
margin-right:-50px;
transition: 1s;

&:hover {
  transition: 1s;
  margin-right:0;

}
}
}

codepen demo

4 个答案:

答案 0 :(得分:1)

这是解决方案。

动画margin属性会导致一些奇怪的结果 - 将其替换为transform

.fixed-icons {
  position: fixed;
  top:40%;
  right:0;

  .fixed-icons__item {
    border: 1px solid white;
    background-color: #004576;
    color: white;
    padding: 0.25rem;
    margin: 0.25rem;
    cursor: pointer;
    margin-right:-50px;
    transition: 1s;

    &:hover {
      transition: 1s;
      transform: translateX(-49px);

    }
  }
}
Here's the solution.


Animating the `margin` property caused some weird results - replaced it with `transform`.

<div class="fixed-icons">
    <div class="fixed-icons__item fixed-icons__item--booking">
        <span>booking</span>
    </div>
    <div class="fixed-icons__item fixed-icons__item--voucher">
        <span>voucher</span>
    </div>
    <div class="fixed-icons__item fixed-icons__item--phone">
        <span>phone</span>
    </div>
    <div class="fixed-icons__item fixed-icons__item--eye">
        <span>eye</span>
    </div>
</div>

<强> Codepen Link

答案 1 :(得分:1)

试试这个,我编辑了你的CSS

&#13;
&#13;
.fixed-icons {
  position: fixed;
  top: 40%;
  right: 0;
}
.fixed-icons .fixed-icons__item {
  border: 1px solid white;
  background-color: #004576;
  color: white;
  padding: 0.25rem;
  margin: 0.25rem;
  cursor: pointer;
  margin-right: -50px;
  transition: 1s;
}
.fixed-icons .fixed-icons__item:hover {
  transition: 1s;
  transform: translateX(-49px);
}
&#13;
<!DOCTYPE html>
<html >
<head>
  <meta charset="UTF-8">
  <title>Create a New Pen</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>
  <div class="fixed-icons">
    <div class="fixed-icons__item fixed-icons__item--booking">
        <span>booking</span>
    </div>
    <div class="fixed-icons__item fixed-icons__item--voucher">
        <span>voucher</span>
    </div>
    <div class="fixed-icons__item fixed-icons__item--phone">
        <span>phone</span>
    </div>
    <div class="fixed-icons__item fixed-icons__item--eye">
        <span>eye</span>
    </div>
</div>
  
  
</body>
</html>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

试试这个,我完全理解你的问题,但据我所知,我已经做到了这一点,试试这可能会帮助你

@import url(http://weloveiconfonts.com/api/?family=entypo);

/* entypo */
[class*="entypo-"]:before {
   font-family: "entypo", sans-serif;
}

a { 
   text-decoration: none;
}

ul {
   list-style: none;
   margin: 0;
   padding: 0;
}

.container {
   margin: 0 auto;
   padding: 20px 50px;
   background:white;
}

#sticky-social {
   left: 0;
   position: fixed;
   top: 50px;
}

#sticky-social a {
   background: #333;
   color: #fff;
   display: block;
   height: 35px;
   font: 16px "Open Sans", sans-serif;
   line-height: 35px;
   position: relative;
   text-align: center;
   width: 35px;
}

#sticky-social a:hover span {
   left: 100%;
}

#sticky-social a span {
   line-height: 35px;
   left: -120px;
   position: absolute;
   text-align:center;
   width:120px;
}

#sticky-social a[class*="facebook"],
#sticky-social a[class*="facebook"]:hover,
#sticky-social a[class*="facebook"] span { background: #3b5998; }

#sticky-social a[class*="twitter"],
#sticky-social a[class*="twitter"]:hover,
#sticky-social a[class*="twitter"] span { background: #00aced; }

#sticky-social a[class*="gplus"],
#sticky-social a[class*="gplus"]:hover,
#sticky-social a[class*="gplus"] span { background: #dd4b39; }	

#sticky-social a[class*="linkedin"],
#sticky-social a[class*="linkedin"]:hover,
#sticky-social a[class*="linkedin"] span { background: #007bb6; }	

#sticky-social a[class*="instagrem"],
#sticky-social a[class*="instagrem"]:hover,
#sticky-social a[class*="instagrem"] span { background: #517fa4; }	

#sticky-social a[class*="stumbleupon"],
#sticky-social a[class*="stumbleupon"]:hover,
#sticky-social a[class*="stumbleupon"] span { background: #eb4924; }	

#sticky-social a[class*="pinterest"],
#sticky-social a[class*="pinterest"]:hover,
#sticky-social a[class*="pinterest"] span { background: #cc2127; }	

#sticky-social a[class*="flickr"],
#sticky-social a[class*="flickr"]:hover,
#sticky-social a[class*="flickr"] span { background: #ff0084; }	

#sticky-social a[class*="tumblr"],
#sticky-social a[class*="tumblr"]:hover,
#sticky-social a[class*="tumblr"] span { background: #32506d; }	
	<div class="container">
		<h1>Social Sidebar</h1>
		<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.</p>
		<p>Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus.</p>
		<p>Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna.</p>
		<p>Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, quis gravida magna mi a libero. Fusce vulputate eleifend sapien. Vestibulum purus quam, scelerisque ut, mollis sed, nonummy id, metus. Nullam accumsan lorem in dui. Cras ultricies mi eu turpis hendrerit fringilla. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; In ac dui quis mi consectetuer lacinia. Nam pretium turpis et arcu. Duis arcu tortor, suscipit eget, imperdiet nec, imperdiet iaculis, ipsum.</p>
		<p>Sed aliquam ultrices mauris. Integer ante arcu, accumsan a, consectetuer eget, posuere ut, mauris. Praesent adipiscing. Phasellus ullamcorper ipsum rutrum nunc. Nunc nonummy metus. Vestibulum volutpat pretium libero. Cras id dui. Aenean ut eros et nisl sagittis vestibulum. Nullam nulla eros, ultricies sit amet, nonummy id, imperdiet feugiat, pede. Sed lectus. Donec mollis hendrerit risus. Phasellus nec sem in justo pellentesque facilisis. Etiam imperdiet imperdiet orci. Nunc nec neque. Phasellus leo dolor, tempus non, auctor et, hendrerit quis, nisi. Curabitur ligula sapien, tincidunt non, euismod vitae, posuere imperdiet, leo. Maecenas malesuada. Praesent congue erat at massa. Sed cursus turpis vitae tortor. Donec posuere vulputate arcu. Phasellus accumsan cursus velit. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed aliquam, nisi quis porttitor congue, elit erat euismod orci, ac</p>
		<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.</p>
		<p>Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus.</p>
		<p>Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna.</p>
		<p>Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, quis gravida magna mi a libero. Fusce vulputate eleifend sapien. Vestibulum purus quam, scelerisque ut, mollis sed, nonummy id, metus. Nullam accumsan lorem in dui. Cras ultricies mi eu turpis hendrerit fringilla. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; In ac dui quis mi consectetuer lacinia. Nam pretium turpis et arcu. Duis arcu tortor, suscipit eget, imperdiet nec, imperdiet iaculis, ipsum.</p>
		<p>Sed aliquam ultrices mauris. Integer ante arcu, accumsan a, consectetuer eget, posuere ut, mauris. Praesent adipiscing. Phasellus ullamcorper ipsum rutrum nunc. Nunc nonummy metus. Vestibulum volutpat pretium libero. Cras id dui. Aenean ut eros et nisl sagittis vestibulum. Nullam nulla eros, ultricies sit amet, nonummy id, imperdiet feugiat, pede. Sed lectus. Donec mollis hendrerit risus. Phasellus nec sem in justo pellentesque facilisis. Etiam imperdiet imperdiet orci. Nunc nec neque. Phasellus leo dolor, tempus non, auctor et, hendrerit quis, nisi. Curabitur ligula sapien, tincidunt non, euismod vitae, posuere imperdiet, leo. Maecenas malesuada. Praesent congue erat at massa. Sed cursus turpis vitae tortor. Donec posuere vulputate arcu. Phasellus accumsan cursus velit. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed aliquam, nisi quis porttitor congue, elit erat euismod orci, ac</p>
	</div>
	<aside id="sticky-social">
		<ul>
			<li><a href="#" class="entypo-facebook" target="_blank"><span>Facebook</span></a></li>
			<li><a href="#" class="entypo-twitter" target="_blank"><span>Twitter</span></a></li>
			<li><a href="#" class="entypo-gplus" target="_blank"><span>Google+</span></a></li>
			<li><a href="#" class="entypo-linkedin" target="_blank"><span>LinkedIn</span></a></li>
			<li><a href="#" class="entypo-instagrem" taget="_blank"><span>Instagram</span></a></li>
			<li><a href="#" class="entypo-stumbleupon" target="_blank"><span>StumbleUpon</span></a></li>
			<li><a href="#" class="entypo-pinterest" target="_blank"><span>Pinterest</span></a></li>
			<li><a href="#" class="entypo-flickr" target="_blank"><span>Flickr</span></a></li>
			<li><a href="#" class="entypo-tumblr" target="_blank"><span>Tumblr</span></a></li>
		</ul>
	</aside>

答案 3 :(得分:0)

容器的方向会导致此问题,您必须设置项目的宽度。 试试这段代码:

&#13;
&#13;
.fixed-icons {
  position: fixed;
  top:40%;
  right:0;
  .fixed-icons__item {
    border: 1px solid white;
    background-color: #004576;
    color: white;
    padding: 0.25rem;
    margin: 0.25rem;
    cursor: pointer;
    margin-right:-50px;
    transition: 1s;
    width:80px;
    &:hover {
      transition: 1s;
      margin-right:0;

    }
  }
}
&#13;
<div style="direction:rtl;">
      <div class="fixed-icons">
          <div class="fixed-icons__item fixed-icons__item--booking">
              <span>booking</span>
          </div>
          <div class="fixed-icons__item fixed-icons__item--voucher">
              <span>voucher</span>
          </div>
          <div class="fixed-icons__item fixed-icons__item--phone">
              <span>phone</span>
          </div>
          <div class="fixed-icons__item fixed-icons__item--eye">
              <span>eye</span>
          </div>
      </div>
  </div>
&#13;
&#13;
&#13;