简体   繁体   English

使用CSS渐变和JavaScript创建动画

[英]Create animation with css gradient and javascript

I am working on an cordova application that allows us to scan rfid tags. 我正在开发可让我们扫描rfid标签的cordova应用程序。 Based of the RSSI signal strength I need to display a bar that shows how close or far the rfid tag is. 基于RSSI信号强度,我需要显示一个条形,以显示rfid标签的接近或远近。 The closer I get to the tag, the higher the bar and the further I go, the smaller the bar becomes. 我越靠近标签,条形越高,我走得越远,条形变得越小。 I built the following plunkr , which I thought I could use. 我构建了以下我认为可以使用的plunkr Unfortunately the animation is not very smooth and its slow on the mobile application. 不幸的是,动画不是很流畅,并且在移动应用程序上运行缓慢。 I think the issue is that I am showing/hiding the divs with a timeout to simulate the animation. 我认为问题在于我正在显示/隐藏具有超时的div以模拟动画。 I think I may need to change this to use css animations with gradients. 我想可能需要更改此设置以使用带有渐变的CSS动画。 But I am not very familiar on how to do this and I have no idea how I would hide part of the gradient if say I want to show just a small part of the bar. 但是我对如何执行此操作不是很熟悉,而且如果说我只想显示栏的一小部分,我也不知道如何隐藏渐变的一部分。 Any suggestions on how to do this are greatly appreciated. 任何有关如何执行此操作的建议,将不胜感激。

Update: I have uploaded a video of what I would like to achieve. 更新:我上传了一段我想要实现的视频。 Unfortunately I am not able to upload this to SO. 不幸的是,我无法将其上传到SO。 The animation would change as I read the tags. 当我阅读标签时,动画将发生变化。 So I could be moving the tag back and forth and based off that it should change the bar. 因此,我可能会来回移动标签,并据此更改标签。

https://www.dropbox.com/s/9bd421fhqvt9v5g/gradient-bar-demo.mov?dl=0 https://www.dropbox.com/s/9bd421fhqvt9v5g/gradient-bar-demo.mov?dl=0

You could animate the height with javascript and the gradient with css. 您可以使用javascript设置高度动画,使用CSS设置渐变动画。 I am updating here on mouse over but you could have it update with every scan input and accomplish the same thing. 我在这里通过鼠标进行更新,但是您可以让它在每次扫描输入时进行更新,并完成相同的操作。 You would just need to set up more css classes for any other gradient variations you want. 您只需要为所需的任何其他渐变变体设置更多的CSS类即可。

 var min = 15; var max = 100 function randPercent(){ return Math.floor(Math.random()*(max-min+1)+min); } $( ".statusBar" ).mouseover(function(){ var barSize = randPercent(); if (barSize > 50) { $(this).addClass('close'); } else { $(this).removeClass('close'); } $(this).animate({ height: barSize+'%' },300 ); }); 
 .wrapper { height:100px; position:relative; } .statusBar { width:50px; min-height:10px; border:1px solid black; position:absolute; bottom:0; } .bg { -webkit-transition: background 1s ; -moz-transition: background 1s ; -ms-transition: background 1s ; -o-transition: background 1s ; transition: background 1s ; background: rgb(71,234,46); } .bg.close { background: rgb(247,247,49); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <div class="statusBar bg"></div> </div> 

For better performance it is best to animate scale rather than height. 为了获得更好的性能,最好是动画缩放而不是高度。 Source (really good article on animation performance). 来源 (关于动画性能的非常好的文章)。

Here is an example of how you could do it. 这是一个如何做的例子。 It's not perfect but hopefully it should give you a good starting point. 它不是完美的,但希望它可以为您提供一个良好的起点。

 var input = document.querySelector('input'); input.addEventListener('input', function() { var value = this.value / 100; var bar = document.querySelector('.meter-bar'); bar.style.transform = 'scaleY(' + value + ')'; // Adjust background size to account for the scale change, there is probably a better solution bar.style.backgroundSize = '200px ' + (300 / value) + 'px'; }); 
 input { width: 200px; } .meter { width: 200px; height: 300px; margin-top: 20px; background-color: #e5e5e5; } .meter-bar { width: 100%; height: 100%; background-color: #333; background-image: linear-gradient(to top, #64b572 0%, #9fc63d 50%, #f63908 100%); background-size: 200px 0; background-position: bottom; transform-origin: bottom; transform: scaleY(0); transition: transform 1s ease, background-size 1s ease; opacity: 0.8; } 
 <input type="range" min="0" max="100" step="1" value="0"> <div class="meter"> <div class="meter-bar"> </div> </div> 

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

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