I'm using bootstrap for the interface of the website I'm developing. I am also planning to integrate the bootswatch themes to my site. I have created a dropdown menu containing the different themes of bootswatch. My problem is how can I switch themes when I click one of the themes in the dropdown menu?
相关问题
- How to add a “active” class to a carousel first el
- Full Clickable Accordion in Bootstrap
- How to add Bootstrap 4 without Tether?
- Laravel overriding bootstrap template
- How to add Labels to Bootstrap dialog footer
相关文章
- Make Bootstrap tab Active on the bases of URL link
- Rails: Twitter Bootstrap Buttons when visited get
- Reduce spacing between rows
- How do use bootstrap tooltips with React?
- Need to design 8 boxes in two rows
- Trigger a Bootstrap .collapse('toggle') vi
- How to override Bootstrap mixin without modifying
- How can I ensure columns wrap equally in Twitter B
you can try something like this
html:
javascript:
where in the setTheme function will be called from your desired source such as dropdown or button.
Fiddle:http://jsfiddle.net/82AsF/ (click the themes dropdown in the navbar)
Give your links a class
theme-link
and adata-theme
value of the theme name like so:Then, define your themes in a global variable like so:
(please host these bootswatch css files on your own server when you do this - I'm only hotlinking because I don't have a server on hand)
Then, use the following jQuery code:
the main problem with above solution if page is refreshed the theme is gone. i found this article very helpful regarding persistent theme in the browser because it save setting in browser's storage.
i hope it helps you
http://wdtz.org/bootswatch-theme-selector.html
The solution provided by Kevin is great. I have just spent some time trying to figure out how I can make it work with local bootswatch libraries.
The typical href attribute value:
But in this case:
I based mine off of @KevinPei's answer to make an automatic dropdown using jQuery, Bootstrap, and Bootswatch to add a dropdown to the navbar that automatically updates the theme to the selected value.
Fiddle: https://jsfiddle.net/davfive/uwseLLzf/show
The code works great in practice. However, in jsfiddle, there are two odd behaviors: