我在向按钮添加新课时遇到问题。
当侧边栏关闭时,按钮箭头应显示右侧(fa-angle-right)。 当它打开时再次离开(fa-angle-left)。
https://jsfiddle.net/tjkzmvLg/
HTML
<button onclick="myFunction1()">Red</button>
<script type="text/javascript">function myFunction1() {
document.body.style.backgroundColor = "red";
}</script>
<button onclick="myFunction2()">Blue</button>
<script type="text/javascript">function myFunction2() {
document.body.style.backgroundColor = "blue";
}</script>
<button onclick="myFunction3()">Green</button>
<script type="text/javascript">function myFunction3() {
document.body.style.backgroundColor = "green";
}</script>
JS
<div>
<button id="sidebarToggle" class="btn btn-primary btn-sm">
<i class="fa fa-angle-left"></i>
</button>
</div>
答案 0 :(得分:1)
一些事情,您应该使用toggleClass来切换类,它更简洁。另外,我使用相对选择器.find
,以便您可以支持多个按钮。为了解决这个问题,你做错了sidebarToggle
。在修正错字后,它起作用了。
(function() {
var $sidebarAndWrapper = $("#sidebar, #wrapper");
$("#sidebarToggle").on("click", function () {
$sidebarAndWrapper.toggleClass("hide-sidebar");
$(this).find("i.fa").toggleClass("fa-angle-left").toggleClass("fa-angle-right");
});
})();
&#13;
/* minified because I only changed JavaScript */
#main{background-color:#aea9a9;padding:4px;margin:0}#footer{background-color:#808080;color:#eee;padding:8px 5px;position:fixed;bottom:0;width:100%}.headshot{max-width:50px;border:1px solid #808080;padding:3px}.menu{font-size:12px;color:aquamarine}.menu li{list-style-type:none}.menu li.active{font-weight:bold}#sidebar{background:#525050;color:aliceblue;position:fixed;height:100%;width:250px;overflow:hidden;left:0;margin:0;transition:left ease .35s}#sidebar.hide-sidebar{left:-250px;transition:left ease .35s}#wrapper{margin:0 0 0 250px;transition:margin-left ease .35s}#wrapper.hide-sidebar{margin-left:0}
&#13;
<!-- minified because I only changed JavaScript -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><body> <div id="sidebar"> <span id="username"></span> <ul class="menu"> <li class="active"><a asp-controller="App" asp-action="Index">Home</a></li><li><a asp-controller="App" asp-action="About">About</a></li><li><a asp-controller="App" asp-action="Contact">Contact</a></li></ul> </div><div id="wrapper"> <div id="main"> <div> <button id="sidebarToggle" class="btn btn-primary btn-sm"> <i class="fa fa-angle-left"></i> </button> </div><div> <h2>The world</h2><p>This will be a fun website soon!</p><form> <div> <label>Date</label> <input/> </div><div> <label>Location</label> <input/> </div><div><input type="submit" value="Add"/></div></form> </div></div><div id="footer"> <h5> © 2017 The World Ltd.</h5> </div></div><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css"> <script type="text/javascript" src="~/js/site.js"></script></body>
&#13;
答案 1 :(得分:1)
你的拼写sidebarToggle
错误-_-
var $icon = $("#sidebarToggle i.fa");