CSS 3 从左过渡滑入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16989585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 23:09:02  来源:igfitidea点击:

CSS 3 slide-in from left transition

csscss-transitions

提问by Gimmy

Is there a cross browser solution to produce a slide-in transition with CSS only, no javascript? Below is an example of the html content:

是否有跨浏览器解决方案来仅使用 CSS 生成滑入式过渡,而不使用 javascript?下面是 html 内容的示例:

<div>
    <img id="slide" src="http://.../img.jpg />
</div>

回答by Christofer Vilander

You can use CSS3 transitions or maybe CSS3 animations to slide in an element.

您可以使用 CSS3 过渡或 CSS3 动画在元素中滑动。

For browser support: http://caniuse.com/

浏览器支持:http: //caniuse.com/

I made two quick examples just to show you how I mean.

我做了两个简单的例子只是为了向你展示我的意思。

CSS transition (on hover)

CSS 过渡(悬停时)

Demo One

演示一

Relevant Code

相关代码

.wrapper:hover #slide {
    transition: 1s;
    left: 0;
}

In this case, Im just transitioning the position from left: -100px;to 0;with a 1s. duration. It's also possible to move the element using transform: translate();

在这种情况下,我只是从转移位置left: -100px;0;了1秒。期间。也可以使用移动元素transform: translate();

CSS animation

CSS动画

Demo Two

演示二

#slide {
    position: absolute;
    left: -100px;
    width: 100px;
    height: 100px;
    background: blue;
    -webkit-animation: slide 0.5s forwards;
    -webkit-animation-delay: 2s;
    animation: slide 0.5s forwards;
    animation-delay: 2s;
}

@-webkit-keyframes slide {
    100% { left: 0; }
}

@keyframes slide {
    100% { left: 0; }
}

Same principle as above (Demo One), but the animation starts automatically after 2s, and in this case I've set animation-fill-modeto forwards, which will persist the end state, keeping the div visible when the animation ends.

原理同上(Demo One),但动画在2s后自动开始,在本例中我设置animation-fill-modeforwards,这将保持结束状态,在动画结束时保持div可见。

Like I said, two quick example to show you how it could be done.

就像我说的,两个快速示例向您展示如何完成。

EDIT:For details regarding CSS Animations and Transitions see:

编辑:有关 CSS 动画和过渡的详细信息,请参阅:

Animations

动画

https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_animations

https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_animations

Transitions

过渡

https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_transitions

https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_transitions

Hope this helped.

希望这有帮助。

回答by mate64

Use CSS3 2D transformto avoid performance issues (mobile)

使用 CSS3 2Dtransform避免性能问题(移动)

A common pitfallis to animate left/top/right/bottomproperties instead of using css-transformto achieve the same effect. For a variety of reasons, the semantics of transforms make them easier to offload, but left/top/right/bottomare much more difficult.

常见缺陷是有生命的left/ top/ right/bottom属性,而不是使用CSS变换,以实现相同的效果。出于各种原因,变换语义使它们更容易卸载,但left/ top/ right/bottom更难。

Source: Mozilla Developer Network (MDN)

来源:Mozilla 开发者网络 (MDN)



Demo:

演示:

var $slider = document.getElementById('slider');
var $toggle = document.getElementById('toggle');

$toggle.addEventListener('click', function() {
    var isOpen = $slider.classList.contains('slide-in');

    $slider.setAttribute('class', isOpen ? 'slide-out' : 'slide-in');
});
#slider {
    position: absolute;
    width: 100px;
    height: 100px;
    background: blue;
    transform: translateX(-100%);
    -webkit-transform: translateX(-100%);
}

.slide-in {
    animation: slide-in 0.5s forwards;
    -webkit-animation: slide-in 0.5s forwards;
}

.slide-out {
    animation: slide-out 0.5s forwards;
    -webkit-animation: slide-out 0.5s forwards;
}
    
@keyframes slide-in {
    100% { transform: translateX(0%); }
}

@-webkit-keyframes slide-in {
    100% { -webkit-transform: translateX(0%); }
}
    
@keyframes slide-out {
    0% { transform: translateX(0%); }
    100% { transform: translateX(-100%); }
}

@-webkit-keyframes slide-out {
    0% { -webkit-transform: translateX(0%); }
    100% { -webkit-transform: translateX(-100%); }
}
<div id="slider" class="slide-in">
    <ul>
        <li>Lorem</li>
        <li>Ipsum</li>
        <li>Dolor</li>
    </ul>
</div>

<button id="toggle" style="position:absolute; top: 120px;">Toggle</button>

回答by w.stoettinger

Here is another solution using css transform (for performance purposes on mobiles, see answer of @mate64 ) without having to use animations and keyframes.

这是使用 css 变换的另一种解决方案(出于移动设备的性能目的,请参阅 @mate64 的答案)而无需使用动画和关键帧。

I created two versions to slide-in from either side.

我创建了两个版本以从任一侧滑入。

$('#toggle').click(function() {
  $('.slide-in').toggleClass('show');
});
.slide-in {
  z-index: 10; /* to position it in front of the other content */
  position: absolute;
  overflow: hidden; /* to prevent scrollbar appearing */
}

.slide-in.from-left {
  left: 0;
}

.slide-in.from-right {
  right: 0;
}

.slide-in-content {
  padding: 5px 20px;
  background: #eee;
  transition: transform .5s ease; /* our nice transition */
}

.slide-in.from-left .slide-in-content {
  transform: translateX(-100%);
  -webkit-transform: translateX(-100%);
}

.slide-in.from-right .slide-in-content {
  transform: translateX(100%);
  -webkit-transform: translateX(100%);
}

.slide-in.show .slide-in-content {
  transform: translateX(0);
  -webkit-transform: translateX(0);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="slide-in from-left">
  <div class="slide-in-content">
    <ul>
      <li>Lorem</li>
      <li>Ipsum</li>
      <li>Dolor</li>
    </ul>
  </div>
</div>

<div class="slide-in from-right">
  <div class="slide-in-content">
    <ul>
      <li>One</li>
      <li>Two</li>
      <li>Three</li>
    </ul>
  </div>
</div>

<button id="toggle" style="position:absolute; top: 120px;">Toggle</button>

回答by Sharavnan Kv

USE THIS FOR RIGHT TO LEFT SLIDING :

使用它向右滑动:

HTML:

HTML:

   <div class="nav ">
       <ul>
        <li><a href="#">HOME</a></li>
        <li><a href="#">ABOUT</a></li>
        <li><a href="#">SERVICES</a></li>
        <li><a href="#">CONTACT</a></li>
       </ul>
   </div>

CSS:

CSS:

/*nav*/
.nav{
    position: fixed;
    right:0;
    top: 70px;
    width: 250px;
    height: calc(100vh - 70px);
    background-color: #333;
    transform: translateX(100%);
    transition: transform 0.3s ease-in-out;

}
.nav-view{
    transform: translateX(0);
}
.nav ul{
    margin: 0;
    padding: 0;
}
.nav ul li{
    margin: 0;
    padding: 0;
    list-style-type: none;
}
.nav ul li a{
    color: #fff;
    display: block;
    padding: 10px;
    border-bottom: solid 1px rgba(255,255,255,0.4);
    text-decoration: none;
}

JS:

JS:

  $(document).ready(function(){
  $('a#click-a').click(function(){
    $('.nav').toggleClass('nav-view');
  });
});

回答by codejockie

I liked @mate64's answer so I am going to reuse that with slight modifications to create a slide down and up animations below:

我喜欢@mate64 的回答,所以我将重用它并稍作修改以创建下面的向下和向上滑动动画:

var $slider = document.getElementById('slider');
var $toggle = document.getElementById('toggle');

$toggle.addEventListener('click', function() {
    var isOpen = $slider.classList.contains('slide-in');

    $slider.setAttribute('class', isOpen ? 'slide-out' : 'slide-in');
});
#slider {
    position: absolute;
    width: 100px;
    height: 100px;
    background: blue;
    transform: translateY(-100%);
    -webkit-transform: translateY(-100%);
}

.slide-in {
    animation: slide-in 0.5s forwards;
    -webkit-animation: slide-in 0.5s forwards;
}

.slide-out {
    animation: slide-out 0.5s forwards;
    -webkit-animation: slide-out 0.5s forwards;
}
    
@keyframes slide-in {
    100% { transform: translateY(0%); }
}

@-webkit-keyframes slide-in {
    100% { -webkit-transform: translateY(0%); }
}
    
@keyframes slide-out {
    0% { transform: translateY(0%); }
    100% { transform: translateY(-100%); }
}

@-webkit-keyframes slide-out {
    0% { -webkit-transform: translateY(0%); }
    100% { -webkit-transform: translateY(-100%); }
}
<div id="slider" class="slide-in">
    <ul>
        <li>Lorem</li>
        <li>Ipsum</li>
        <li>Dolor</li>
    </ul>
</div>

<button id="toggle" style="position:absolute; top: 120px;">Toggle</button>