How to dynamically change themes after clicking a drop down menu of themes

前端 未结 6 1160
甜味超标
甜味超标 2020-12-12 18:01

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 conta

相关标签:
6条回答
  • 2020-12-12 18:36

    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"
        }
    
    0 讨论(0)
  • 2020-12-12 18:42

    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>
    
    0 讨论(0)
  • 2020-12-12 18:43

    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

    0 讨论(0)
  • 2020-12-12 18:46

    Thanks at @davfive and his copy/paste one file sample.
    Here are his sample upgraded to bootswatch 4.5 and jQuery 3.5 :

    <html lang="en">
    <head>
        <meta charset="utf-8" />
        <!-- To switch themes, go to https://www.bootstrapcdn.com/bootswatch/?theme=0 -->
        <link href="https://maxcdn.bootstrapcdn.com/bootswatch/4.5.0/flatly/bootstrap.min.css" rel="stylesheet">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
    </head>
    <body>
        <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
            <a class="navbar-brand" href="index.html"><img src="images/logos/Logo.png" alt="logo">MyWebSite Name </a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01" aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
    
            <div class="collapse navbar-collapse" id="navbarColor01">
                <ul class="navbar-nav mr-auto">
                    <li class="nav-item active">
                        <a class="nav-link" href="index.html">home<span class="sr-only">(current)</span></a>
                    </li>
                    <li id="theme-button" class="dropdown">
                        <a href="#"  class="dropdown-toggle nav-link" data-toggle="dropdown">Change Theme <b class="caret"></b></a>
                        <ul class="dropdown-menu ">
                            <li><a href="#" name="current">Flatly</a></li>
                            <li class="divider"></li>
                            <li><a href="#" name="flatly">Flatly</a></li>
                            <li><a href="#" name="superhero">Superhero</a></li>
                            <li><a href="#" name="united">United</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </nav>
    <script>
    jQuery(function($) {
      $('#theme-button ul a').click(function() {
        if ($(this).attr('name') != 'current') {
          var urlbeg = 'https://maxcdn.bootstrapcdn.com/bootswatch/4.5.0/'
          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>
    

    I also add home menu link and remove a lot of themes. Of course, you can add/change some.

    0 讨论(0)
  • 2020-12-12 18:58

    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.

    0 讨论(0)
  • 2020-12-12 19:03

    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);
        });
    });
    
    0 讨论(0)
提交回复
热议问题