【问题标题】:replace Select tag with toggle switch用拨动开关替换 Select 标签
【发布时间】:2018-05-03 06:22:04
【问题描述】:

我在项目中使用选择标签来更改网站的主题。这在我的项目中就像一个魅力,但我想用带有 jquery 集成的 toggle switch 替换 select 标记。就像,youtube 切换黑暗主题。 我该怎么做呢

这是我的代码。

HTML、JS 和 JQuery

$(function() {
  $('.style-change').change(function() {

    var style = $(this).val();
    $('body').fadeOut("slow", function() {
      $('link[rel="stylesheet"]').attr("href", style + ".css");
      $('body').fadeIn("slow");

      $.cookie("css", style, {
        expires: 365,
        path: '/'
      });
    });
  });
});

$(document).ready(function() {
  $("#style").val('<?php echo $style; ?>');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>
<select name="" class="style-change form-control" id="style">
  <option value="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min">Default Style</option>
  <option value="https://bootswatch.com/3/darkly/bootstrap.min">Night Mode</option>
</select>

PHP

<?php
if (isset($_COOKIE['css'])) {
    $style = $_COOKIE['css'];
} else {
    $style = 'default';
}
?>

我正在使用 jQuery cookie 库将用户设置保存在 cookie 中。

任何建议都将是可观的。

A DEMO page

【问题讨论】:

  • 可以使用复选框进行切换开关。您确定要仅使用选择标签吗?
  • 我想用切换开关替换选择标签,比如 youtube 深色主题切换。 @abhiox

标签: php jquery html css


【解决方案1】:

参考这个,希望对你有帮助 - https://www.jqueryscript.net/form/jQuery-Plugin-To-Convert-Select-Box-Into-A-Switch-Control-Switch-Button.html

基本用法:

  1. 在文档中加载jQuery库和jQuery切换按钮插件。

  2. 创建一个常规选择框。

      <option value="1">Item One</option>
    
      <option value="2">Item Two</option>
    
      <option value="3">Item Three</option>
    
      <option value="4" selected>Item Four</option>
    
    
    </select>
    
  3. 调用插件将其转换为可切换的开关按钮。

    $('.btn-switch').switchButton();

  4. 将您的自定义样式添加到此切换按钮。

    .switch-select {

      overflow: hidden;
    
      border-radius: 3px;
    
    }
    
    
    
    .switch-select-button {
    
      text-align: center;
    
      font-size: 80%;
    
      padding: 5px;
    
      cursor: pointer;
    
      background: rgb(224, 224, 224);
    
    }
    
    
    .switch-select-button:hover {
    
      background: rgb(207, 55, 55);
    
      color: white;
    
    }
    
  5. 可用选项。

    template: "", // 自定义模板

    class: "switch-select", // element class
    
    innerclass: "switch-select-button", // inner element class
    
    width: "", // default to its parent width
    
    marker: "#3577EE, // marker color
    
    markerback: "#B9B9B9", // marker background color
    
    markerh: "4px", // marker size
    
    onchange: "function(value){}" // callback function
    

【讨论】:

【解决方案2】:

您可以使用复选框并使用纯 CSS 进行切换。

.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {
  display: none;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  transition: .4s;
  border-radius: 34px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  transition: .4s;
  border-radius: 50%;
}

input:checked+.slider {
  background-color: #2196F3;
}

input:focus+.slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked+.slider:before {
  transform: translateX(26px);
}
<label class="switch">
  <input type="checkbox" checked>
  <span class="slider round"></span>
</label>

【讨论】:

  • 先生,如何与 jQuery 集成? @杰拉德
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-16
  • 1970-01-01
  • 2016-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多