简体   繁体   English

在CSS转换期间显示元素的大小值

[英]Show size value of element during CSS transition

When a user scroll to certain element on my page I add class which starts an animation of the width of it. 当用户滚动到我页面上的某个元素时,我添加了一个类,该类启动了它的宽度动画。 I starts of with a width of 0% then goes up to 99% for example. 我开始宽度为0%,然后达到99%。 As this animates is there a way to display this width incrementing in the page in HTML ie a <p> tag with this value incrementing? 因为这个动画有一种方法可以在HTML页面中显示这个宽度递增,即一个<p>标签,这个值递增吗? The CSS is just this, I add the class .active when the user scrolls to it with Javascript. CSS就是这样,当用户使用Javascript滚动它时,我添加了类.active。

.graph-horiz-bar__bar{
  background:$niagara;
  top:0px;
  position:absolute;
  left:0px;
  width:0;
  height:100%;
  transition: width 0.3s;
}
.graph-horiz-bar__bar.active{
  width:100%;
  transition: width 0.3s;
}
var div = document.getElementById("yourDiv"), parapgraph = document.getElementById("yourParagraph");  
setInterval(function(){
    paragraph.innerHTML = div.offsetWidth + "px";
}, 20);

This sets the paragraphs content to the div's width every 20 ms. 这会将段落内容设置为每20毫秒div的宽度。

The simplest way would be to animate the element width using jQuery .animate() and reading the current width from the step: parameter callback (Read the Docs)... 最简单的方法是使用jQuery .animate()设置元素宽度的动画,并从step:参数回调中读取当前宽度(阅读文档)...

Using CSS3 transition: 使用CSS3过渡:

 var $bar = $("#bar"), $currWidth = $("#currWidth"), itv = null; $("#bar").on({ // START COUNTING THE WIDTH // I used a custom "start" event cause currently (2016) // Event.transitionstart is implemented only in IE10+, Edge start : function(){ $(this).addClass("active"); itv = setInterval(function(){ $currWidth.text($bar[0].offsetWidth); },10); }, // STOP COUNTING THE WIDTH transitionend : function() { clearInterval(itv); $currWidth.text("INTERVAL STOPPED"); } }); $("#start").on("click", function(){ // CLICK JUST FOR DEMO, // You need to place this trigger inside your inViewport method // when the element enters the viewport $bar.trigger("start"); // <<---------------- }); 
 #bar{ height: 20px; width:0; background:red; transition: 3s; } #bar.active{ width:100%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="start">ACTIVATE BAR</button><!-- TRIGGER BUTTON JUST FOR DEMO --> width <span id="currWidth">0</span> <div id="bar"></div> 


When transitionstart will be implemented by all major browsers than the code would look pretty much like: transitionstart将由所有主要浏览器实现时,代码看起来非常像:

var itv;

$("#bar").on({
  transitionstart: function(){
    itv = setInterval(function(){
      console.log( $bar[0].offsetWidth );
    },10);
  },
  transitionend : clearInterval(itv)
});

$("#bar").addClass("active"); // place this call where needed

Probably some day in another galaxy some event like transitionstep could be all it takes.... 可能有一天在另一个星系中,像transitionstep这样的事件可能只需要......

$("#bar").on("transitionstep", function(){ // :( 
   console.log( this.offsetWidth );
});

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

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