简体   繁体   English

如何动态更新我的CSS,即我想让用户选择字体大小,bckground颜色

[英]How can I dynamically update my CSS i.e. I want to give user an option of selecting font size, bckground color

Hi mine is a Java spring project, I want to give user an option of changing font size, background color and window size at run time ie after login in into system. 我的是一个Java spring项目,我希望用户可以选择在运行时更改字体大小,背景颜色和窗口大小,即登录系统后。 The project contains multiple css pages and is called in .js pages. 该项目包含多个css页面,并在.js页面中调用。

I have got this solution on goggle but it raised some other questions: 我有一个关于护目镜的解决方案,但它提出了一些其他问题:

Solution A:We can use the document.createElement function to create a new style element. 解答A:我们可以使用document.createElement函数来创建一个新的样式元素。 This is useful when we want to give site visitors the option of changing your site styles dynamically, using some button controls. 当我们想让网站访问者使用某些按钮控件动态更改网站样式时,这非常有用。

Example: 例:

var sheet = document.createElement('style')
sheet.innerHTML = "div {border: 2px solid black; background-color: blue;}";
document.body.appendChild(sheet);

In this above statement it creates a css style sheet that time, and later on we can remove style sheet in the same code. 在上面的语句中,它创建了一个时间的css样式表,稍后我们可以在相同的代码中删除样式表。 Switching between different style sheets based on user preference, we can set up multiple style sheets and enable only the ones that the current site visitor would want to view. 根据用户偏好在不同样式表之间切换,我们可以设置多个样式表,并仅启用当前站点访问者想要查看的样式表。

The questions I get here are: 我在这里得到的问题是:

  1. What would happen to the created style sheet once the used logged out, will it stay on the cache? 一旦使用的注销,创建的样式表会发生什么,它会保留在缓存中吗?
  2. If it stays on the cache, would there be multiple style sheet per user depending on the number of times he change the interface setting or just one per user? 如果它保留在缓存中,每个用户会有多个样式表,具体取决于他更改界面设置的次数还是每个用户只有一个?
  3. How would this behave if the user is in incognito mode? 如果用户处于隐身模式,这会如何表现?

Or if someone can suggest me any other feasible solution. 或者如果有人可以建议我任何其他可行的解决方案 I am working as an intern and this is the first task I am assigned. 我是一名实习生,这是我分配的第一项任务。 TIA TIA

There are multiple ways to do it. 有多种方法可以做到这一点。 And there are too many questions unanswered. 有太多问题没有答案。

What I am assuming is you are using a spring back-end and your page is not a single page app. 我假设您正在使用弹簧后端,而您的页面不是单页应用程序。

  1. If the user logs out will your style sheet be saved in the cache ? 如果用户注销,您的样式表是否会保存在缓存中?

    It depends on how you are logging out. 这取决于你如何退出。 If you are redirecting to a different page on logout, the page that you created is no longer available and the changes you made on the page won't be cached unless you save your changes in the cache using service worker or if the whole set up is a single page app. 如果在注销时重定向到其他页面,则您创建的页面将不再可用,并且除非您使用服务工作者将更改保存在缓存中,或者整个设置完成,否则不会缓存您在页面上所做的更改是一个单页面的应用程序。

  2. If it stays on the cache, would there be multiple style sheet per user depending on the number of times he change the interface setting or just one per user? 如果它保留在缓存中,每个用户会有多个样式表,具体取决于他更改界面设置的次数还是每个用户只有一个?

    Again it depends on how you are creating the stylesheet. 同样,这取决于您如何创建样式表。 you can delete your existing stylesheet and then insert your new stylesheet. 您可以删除现有的样式表,然后插入新的样式表。 In this way there will always be only one stylesheet per user. 通过这种方式,每个用户始终只有一个样式表。

  3. How would this behave if the user is in incognito mode? 如果用户处于隐身模式,这会如何表现?
    If you are not caching using service worker then this wont have any impact. 如果您不使用服务工作者进行缓存,那么这不会产生任何影响。

What you can do is 你能做的是

  1. Save user preferences as json values and then create stylesheets using that json values 将用户首选项保存为json值,然后使用该json值创建样式表
  2. Once user logs off clear the existing stylesheets. 用户注销后清除现有样式表。
  3. If the same user logs in again check for the json values present, if it's present then generate the style sheets again or else fetch the preference from the server and then generate the stylesheet 如果同一用户再次登录,请检查存在的json值,如果存在,则再次生成样式表,或者从服务器获取首选项,然后生成样式表

暂无
暂无

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

相关问题 如何在不轮询的情况下检测某个DIV的大小已更改? 即,我想要调整大小的事件监听器? - How can I detect when a certain DIV has changed size without polling? I.e., I want a resize event listener? 如何强制在JSP(即不是PHP)上重新加载缓存的css文件? - How can I force a cached css file to be reloaded on a JSP (i.e. not PHP)? 如何通过动画动态加载页面元素(即图像)? - How to dynamically load in page elements (i.e. images) with an animation? 如何在 setState 之后在 React 中强制立即更新 DOM(即提交)? - How can I force an immediate DOM update (i.e. commit) in React after setState? 如何获得屏幕的物理尺寸(即英寸)? - How to get screen's physical size (i.e. in inches)? 如何使用Javascript动态增加字体大小,为什么当前的功能不起作用? - How can I dynamically increase font size with Javascript and why is my current function not working? 默认情况下,如何使 *ngFor 中的第一个项目处于活动状态(即活动项目的边框颜色默认应与其他项目不同) - How can I make the first item in my *ngFor active by default (i.e. border-color of active item should be differnt from other items by deafult) 如何使用数据表动态更改行的字体大小? - How can I change the font size of a row dynamically with datatables? 如何动态更新我的对象? - How can I update my object dynamically? 如何获取由css设置的元素的字体大小 - How can I get the font size of an element as it was set by css
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM