Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

javascript - Dynamically change css attribute works in regular browser mode but does not work in PWA mode

I am using vue.js and cordova to build an application so it can be used on all platforms, Android, iPhone, PWA, and as regular web apps as well. One feature I implemented is to let users customize the theme color, and I use the following function to set color dynamically.

This works fine on android app, iPhone app, in browser, but it does not work in PWA mode (load the site in browser, and then add to home screen).

Can someone help diagnose the problem?

   setColor: function(v){
     [].every.call(document.styleSheets, function (sheet) {
       return [].every.call(sheet.cssRules, function (rule) {
         if (rule.selectorText == '.bg-main') {
           rule.style.setProperty('background-color', v, 'important');
         }
         if (rule.selectorText == '.btn-main') {
           rule.style.setProperty('background-color', v, 'important');
           rule.style.setProperty('border-color', v, 'important');
         }
         if (rule.selectorText == '.btn-outline-main') {
           rule.style.setProperty('border-color', v, 'important');
         }
         if (rule.selectorText == '.active' || rule.selectorText=='.text-main') {
           rule.style.setProperty('color', v, 'important');
         }
         if (rule.selectorText == '.custom-control-input:checked ~ .custom-control-label::before') {
           rule.style.setProperty('border-color', v, 'important');
           rule.style.setProperty('background-color', v, 'important');
         }
         return true;
       });
     });
   }

a snippet of my css style is below

  .btn-main {
    background-color: #7a050b !important;   /* #B20900 */
    border-color: #7a050b !important;
    color: white !important;
  }
question from:https://stackoverflow.com/questions/66052847/dynamically-change-css-attribute-works-in-regular-browser-mode-but-does-not-work

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...