How to dynamically change themes after clicking a

2019-01-31 01:33发布

问题:

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?

回答1:

Fiddle:http://jsfiddle.net/82AsF/ (click the themes dropdown in the navbar)
Give your links a class theme-link and a data-theme value of the theme name like so:

<a href="#" data-theme="amelia" class="theme-link">Amelia</a>

Then, define your themes in a global variable like so:

var themes = {
    "default": "//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css",
    "amelia" : "//bootswatch.com/amelia/bootstrap.min.css",
    "cerulean" : "//bootswatch.com/cerulean/bootstrap.min.css",
    "cosmo" : "//bootswatch.com/cosmo/bootstrap.min.css",
    "cyborg" : "//bootswatch.com/cyborg/bootstrap.min.css",
    "flatly" : "//bootswatch.com/flatly/bootstrap.min.css",
    "journal" : "//bootswatch.com/journal/bootstrap.min.css",
    "readable" : "//bootswatch.com/readable/bootstrap.min.css",
    "simplex" : "//bootswatch.com/simplex/bootstrap.min.css",
    "slate" : "//bootswatch.com/slate/bootstrap.min.css",
    "spacelab" : "//bootswatch.com/spacelab/bootstrap.min.css",
    "united" : "//bootswatch.com/united/bootstrap.min.css"
}

(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:

$(function(){
   var themesheet = $('<link href="'+themes['default']+'" rel="stylesheet" />');
    themesheet.appendTo('head');
    $('.theme-link').click(function(){
       var themeurl = themes[$(this).attr('data-theme')]; 
        themesheet.attr('href',themeurl);
    });
});


回答2:

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



回答3:

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:

  1. Sometimes the dropdown extends to the whole screen
  2. The dropdown's mouseover/hover color isn't showing in jsfiddle.

<html lang="en">
<head>
    <!-- To switch themes, go to https://www.bootstrapcdn.com/bootswatch/?theme=0 -->
    <link href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/cerulean/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="navbar navbar-default navbar-static-top">
  <div class="container-fluid">
    <a class="navbar-brand">Bootswatch Theme Changer</a>
    <div class="nav navbar-nav pull-right">
        <li id="theme-button" class="dropdown">
          <a href="#"  class="dropdown-toggle" data-toggle="dropdown">Change Theme <b class="caret"></b></a>
          <ul class="dropdown-menu ">
            <li><a href="#" name="current">Cerulean</a></li>
            <li class="divider"></li>
            <li><a href="#" name="cerulean">Cerulean</a></li>
            <li><a href="#" name="cosmo">Cosmo</a></li>
            <li><a href="#" name="cyborg">Cyborg</a></li>
            <li><a href="#" name="darkly">Darkly</a></li>
            <li><a href="#" name="flatly">Flatly</a></li>
            <li><a href="#" name="journal">Journal</a></li>
            <li><a href="#" name="lumen">Lumen</a></li>
            <li><a href="#" name="paper">Paper</a></li>
            <li><a href="#" name="readable">Readable</a></li>
            <li><a href="#" name="sandstone">Sandstone</a></li>
            <li><a href="#" name="simplex">Simplex</a></li>
            <li><a href="#" name="slate">Slate</a></li>
            <li><a href="#" name="solar">Solar</a></li>
            <li><a href="#" name="spacelab">Spacelab</a></li>
            <li><a href="#" name="superhero">Superhero</a></li>
            <li><a href="#" name="united">United</a></li>
            <li><a href="#" name="yeti">Yeti</a></li>
          </ul>
        </li>
    </div>
  </div>
</div>
<script>
jQuery(function($) {
  $('#theme-button ul a').click(function() {
    if ($(this).attr('name') != 'current') {
      var urlbeg = 'https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/'
      var urlend = '/bootstrap.min.css'
      var themeurl = urlbeg + $(this).text().toLowerCase() + urlend;
      var link = $('link[rel="stylesheet"][href$="/bootstrap.min.css"]').attr('href', themeurl);

      $('#theme-button ul a[name="current"]').text($(this).text());
    }
  });
});
</script>
</body>
</html>


回答4:

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:

"~/lib/bootstrap/dist/css/bootstrap.min.css"

But in this case:

        var themes = {

        "default": "lib/bootstrap/dist/css/bootstrap.min.css",
        "amelia": "lib/bootswatch/amelia/bootstrap.min.css",
        "cerulean": "lib/bootswatch/cerulean/bootstrap.min.css",
        "cosmo": "lib/bootswatch/cosmo/bootstrap.min.css",
        "cyborg": "lib/bootswatch/cyborg/bootstrap.min.css",
        "flatly": "lib/bootswatch/flatly/bootstrap.min.css",
        "journal": "lib/bootswatch/journal/bootstrap.min.css",
        "readable": "lib/bootswatch/readable/bootstrap.min.css",
        "simplex": "lib/bootswatch/simplex/bootstrap.min.css",
        "slate": "lib/bootswatch/slate/bootstrap.min.css",
        "spacelab": "lib/bootswatch/spacelab/bootstrap.min.css",
        "united": "lib/bootswatch/united/bootstrap.min.css"
    }


回答5:

you can try something like this

html:

<link href="/Themes/DefaultClean/Content/css/styles.css" id="theme-sheet" rel="stylesheet" type="text/css" />

javascript:

function setTheme(themeName) {
    $('#theme-sheet').attr({ href: "https://bootswatch.com/slate/bootstrap.min.css" });;
}

where in the setTheme function will be called from your desired source such as dropdown or button.