Change the fill of an svg as a progress bar passes in the background

廉价感情. 提交于 2019-12-25 03:38:26

问题


I have a progress bar that drops in when the user scrolls down more than 100 pixels. The background of the progress bar fills with red. The progress bar container also contains an svg of a tree which is white.

How can I change the fill color of the svg as the progress bar passes behind it. So, if the progress bar is halfway through the back of the tree, the left half would be white and the right half would still be red. Is that even possible? I am using jQuery currently.

I mocked up a quick pen here: http://codepen.io/redbranchmedia/pen/aGfme

Here is the script I am using for the progress bar:

  // fills progress bar on scrolldown 

jQuery(document).on('ready', function() {  
  var winHeight = $(window).height(), 
      docHeight = $(document).height(),
      progressBar = $('progress'),
      max, value;

  /* Set the max scrollable area */
  max = docHeight - winHeight;
  progressBar.attr('max', max);

  jQuery(document).on('scroll', function(){
     value = $(window).scrollTop();
     progressBar.attr('value', value);
  });
});
// end progress bar

回答1:


You're almost there. It is an animation trick to use a red tree behind a white tree. So when the white tree is unmasked, the red tree appears.

Earlier revision answers the SO question, however it isn't 100% pixel accurate with various window width and window zoom (May be possible to fix it with more math). Hence this new update below.

Update: New codepen example using gradient to gradually turn it white. Adjust the value 0.1 in (max*0.1) if you want it to turn white later



来源:https://stackoverflow.com/questions/26188696/change-the-fill-of-an-svg-as-a-progress-bar-passes-in-the-background

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!