平滑的动画以进行svg填充

时间:2019-06-28 10:15:44

标签: javascript jquery css svg svg-animate

我有一个SVG温度计,我正在尝试为每个课程的填充动画制作动画。

我用切换类做了温度计。我正在尝试为每个班级的上下平滑动画,以及上下动画。

https://lore1ei.github.io/-温度计。

var firstStop = document.getElementById('F1gst1');
percentage = '0%';
firstStop.setAttribute('offset', percentage);
var CountAllCheckboxes = $('.analysis-li').length;
var CountChecked = 0;

$(".analysis-li").click(function() {
  $(this).toggleClass("check");

  CountChecked = $('.analysis-li.check').length;
  percentage = ((CountChecked / CountAllCheckboxes) * 100) + '%';
  firstStop.setAttribute('offset', percentage);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>

<svg class="thermometr" id="Layer_1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 44.3 333.8">
  <linearGradient y2="0%" x2="0%" y1="100%" x1="0%" id="F1g">
    <stop stop-color="#00FF00" offset="0%" id="F1gst1"/>
    <stop stop-color="#FFFFFF" offset="0%" id="F1gst2"/> 
  </linearGradient>
  <path fill="url(#F1g)" class="st0" d="M30.5 297.5V4.6c0-2.5-2.1-4.6-4.6-4.6-2.5 0-4.6 2.1-4.6 4.6v292.9c-7.9 2-13.8 9.2-13.8 17.8 0 10.2 8.2 18.4 18.4 18.4s18.4-8.2 18.4-18.4c0-8.5-5.9-15.7-13.8-17.8"/>
  <path fill="url(#F1g)" class="st0"  d="M9 290.2h7.5v.5H9zM9 284.3h7.5v.6H9zM9 278.4h7.5v.5H9zM9 272.5h7.5v.6H9zM0 266.6h16.5v.6H0zM9 260.7h7.5v.5H9zM9 254.8h7.5v.6H9zM9 248.9h7.5v.5H9zM9 243h7.5v.6H9zM0 237.1h16.5v.6H0zM9 231.3h7.5v.5H9zM9 225.4h7.5v.6H9zM9 219.5h7.5v.6H9zM9 213.6h7.5v.6H9zM0 207.7h16.5v.6H0zM9 201.8h7.5v.6H9zM9 195.9h7.5v.6H9zM9 190h7.5v.6H9zM9 184.1h7.5v.5H9zM0 178.2h16.5v.6H0zM9 172.3h7.5v.6H9zM9 166.4h7.5v.5H9zM9 160.5h7.5v.6H9zM9 154.7h7.5v.6H9zM0 148.8h16.5v.6H0zM9 142.9h7.5v.6H9zM9 137h7.5v.5H9zM9 131.1h7.5v.5H9zM9 125.2h7.5v.6H9zM0 119.3h16.5v.5H0zM9 113.4h7.5v.6H9zM9 107.5h7.5v.6H9zM9 101.6h7.5v.5H9zM9 95.7h7.5v.6H9zM0 89.8h16.5v.6H0zM9 83.9h7.5v.6H9zM9 78.1h7.5v.6H9zM9 72.2h7.5v.6H9zM9 66.3h7.5v.6H9zM0 60.4h16.5v.6H0zM9 54.8h7.5v.6H9zM9 48.9h7.5v.6H9zM9 43h7.5v.5H9zM9 37.1h7.5v.6H9zM0 31.2h16.5v.5H0zM9 26h7.5v.6H9zM9 20.1h7.5v.5H9zM9 14.2h7.5v.6H9zM9 8.3h7.5v.6H9zM0  2.4h16.5V3H0z"/>
</svg>

3 个答案:

答案 0 :(得分:0)

一种解决方案是仅使用javascript而不是使用CSS和/或svg-props对其进行动画处理。例如:

function anim(fromPercentage) {
   var topLimit = 25;  
   fromPercentage += 1;
   if(fromPercentage < topLimit) {
      $('#F1gst1').attr('offset', fromPercentage + '%');
      setTimeout( function() {
         anim(fromPercentage);
      }, 35);
   }
};

/*
* Animates the bar from 10 to 25 percentage.
* Change the topLimit inside the anim-function to the current desired top level.
* Also tweek the timeout to get smoother transition.
*/
anim(10);

答案 1 :(得分:0)

  

@Thomas但是我需要((CountChecked / CountAllCheckboxes)* 100)+'%'。将会有太多的遮罩和功能。

这是一个面具和一个功能,这是一个简短的片段:

const scale = Array.from(Array(101), (_,i,a) => 90*(100-i)/100);
document.querySelector("svg #scale").setAttributeNS(null, "d", scale.filter((v,i) => i && i<100 && i%10 === 0).map(v => `M20,${v},25,${v}`).join(""));

setInterval(function(){
  let percent = Math.floor(Math.random() * 100);
  document.querySelector("div").textContent = percent + "%";
  document.querySelector("svg #red").style.setProperty("transform", `translateY(${scale[percent]}px)`)
}, 1000);
svg {
  max-width: 100px;
  max-height: 200px;
}

svg #red {
  transition: transform 300ms;
}
<div></div>
<svg viewbox="0,0,50,100">
<mask id="myMask">
  <line x1=25 y1=5 x2=25 y2=80 stroke="white" stroke-width="10" stroke-linecap="round" />
  <circle cx=25 cy=90 r=10 fill="white" />
</mask>

<g mask="url(#myMask)">
  <rect x=0 y=0 width=50 height=100 fill="#AAA" />
  <rect id="red" x=0 y=0 width=50 height=100 fill="red" />
  <path id="scale" stroke="#888" stroke-width="0.5" d=""/>
</g>
</svg>

答案 2 :(得分:0)

(function() {
    const btnChange = document.getElementById('btn-change');
    const containerDynamic = document.getElementById('container-dynamic');
    
    let toggle = true;
    
    btnChange.addEventListener('click', function(){
        if(toggle) {
            if(btnChange) {
                btnChange.style.backgroundColor = "#22a7f0";
                btnChange.style.color = "#fff";
                btnChange.textContent = "Login";
                getSign();
                toggle = false;
            }
        } else {
            if(btnChange) {
                btnChange.style.backgroundColor = "#fff";
                btnChange.style.color = "#22a7f0";
                btnChange.textContent = "Sign Up";
                getLog();
                toggle = true;
            }
        }
    });
    
    function getSign() {
        if(containerDynamic) {
            let xhr1 = new XMLHttpRequest();
            xhr1.onreadystatechange = function() {
                if(xhr1.readyState == 4 && xhr1.status == 200) {
                    containerDynamic.innerHTML = xhr1.response;
                }
            };
            xhr1.open('get','site/signup.php',true);
            xhr1.send();
        }
    };
    
    function getLog() {
        if(containerDynamic) {
            let xhr2 = new XMLHttpRequest();
            xhr2.onreadystatechange = function() {
                if(xhr2.readyState == 4 && xhr2.status == 200) {
                    containerDynamic.innerHTML = xhr2.response;
                }
            };
            xhr2.open('get','site/mheader.php',true);
            xhr2.send();
        }
    };
})();

这是我朋友的解决方案。

<!DOCTYPE html>
<html>
    <head lang="en">
        <title>Something</title>
        <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale = 1.0, maximum-scale=1">
        <link href="https://fonts.googleapis.com/css?family=Lato:300&display=swap" rel="stylesheet">
        <link rel="stylesheet" type="text/css" href="resources/css/mstyle.css">
    </head>
    <body>
        <div id="container">
            <div id="logo-hiw">
                <img id="logo" src="resources/css/img/logo.png">
                <img id="hiw-symbol" src="resources/css/img/help.svg">
            </div>
            <div id="container-dynamic">
                <?php
                    require "site/mheader.php";
                ?>
            </div>
            <div id="or">
                <p>OR</p>
                <div class="link">
                    <button id="btn-change" class="button">Sign Up</button>
                </div>
            </div>
        </div>
        <script type="text/javascript" src="resources/js/mscript.js"></script>
    </body>
</html>