简体   繁体   English

除了悬停建议之外的css过渡效果onload

[英]css transition effect onload other than hover suggestions

I have been playing around with css transition effects. 我一直在玩css过渡效果。 All is working but trying different properties other than :hover . 一切正在发挥作用,但尝试不同的属性:hover

I am looking for something that transition the div onload, for instance so if I am on the page and I am scrolling down and as soon as I reach the certain div then the transition property applies. 我正在寻找转换div onload的东西,例如,如果我在页面上并且我向下滚动,一旦到达某个div,则转换属性适用。

Here is snippet I have been playing around with to get the transition started when hovering over the element, looking for something similar but that loads automatically without hovering: 这是我一直在玩的片段,当悬停在元素上时开始转换,寻找类似的东西但是自动加载而不悬停:

.box {
    border-style: solid;
    border-width: 1px;
    display: block;
    width: 760px;
    height: 760px;
    background-image: url("1.png");
    -webkit-transition: width 2s, height 2s, background-color 2s, -webkit-transform 2s;
    transition: width 2s, height 2s, background-color 2s, transform 2s;
    padding-bottom:20px;
}

.box:hover {
    background-image: url("1.png");
    width: 760px;
    height: 760px;
    -webkit-transform: rotate(180deg);
    transform: rotate(-2deg);
    padding-bottom:20px;
}

You will need to use a little Javascript to append a certain class with the appropriate transition properties to your .box container to apply transition on load. 您需要使用一些Javascript将具有相应过渡属性的特定类附加到.box容器,以便在加载时应用转换。

Note that a setTimeout needs to be applied to append the class so that the transition is visible, otherwise the properties just override. 请注意,需要应用setTimeout来附加类,以便转换可见,否则属性会覆盖。

Refer the following demo: 请参阅以下演示:

 setTimeout(function() { document.getElementsByClassName("box")[0].classList.add("transitionBox"); }, 100); 
 .box { border-style: solid; border-width: 1px; display: block; width: 760px; height: 760px; background-image: url("1.png"); -webkit-transition: width 2s, height 2s, background-color 2s, -webkit-transform 2s; transition: width 2s, height 2s, background-color 2s, transform 2s; padding-bottom: 20px; } .transitionBox { background-image: url("1.png"); width: 760px; height: 760px; -webkit-transform: rotate(180deg); transform: rotate(-2deg); padding-bottom: 20px; } 
 <div class="box"></div> 

You can make annimation css , automaticly by adding a class to that div , so whether it's :hover or have a class annimation it'll trigger : (use setTimeout to define time to start end animmation ) 您可以通过向该div添加一个类来自动生成annimation css,因此无论是:hover还是具有类annimation它都会触发:(使用setTimeout定义开始结束动画的时间)

bellow a working snippet : 吼叫一个工作片段:

 $(function() { setTimeout(function() { $(".box").addClass("annimation"); }, 500) setTimeout(function() { $(".box").removeClass("annimation"); }, 3000) }) 
 .box { border-style: solid; border-width: 1px; display: block; width: 360px; height: 360px; background-image: url("http://www.wallpaperstop.com/wallpapers/flower-wallpapers/flower-imag-336x210-0111.jpg"); -webkit-transition: width 2s, height 2s, background-color 2s, -webkit-transform 2s; transition: width 2s, height 2s, background-color 2s, transform 2s; padding-bottom: 20px; } .box.annimation, .box:hover { -webkit-transform: rotate(180deg); transform: rotate(180deg); padding-bottom: 20px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="box"> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM