admin管理员组文章数量:1291675
Using the awesome ionic framework, I'm using the Button bar like the pic below :
But I want when I press a button, it should remain pressed and the others off, and when I press the second, it remains pressed and the others off.
like this :
here is the code :
<div class="button-bar">
<a class="button">First</a>
<a class="button">Second</a>
<a class="button">Third</a>
</div>
Using the awesome ionic framework, I'm using the Button bar like the pic below :
But I want when I press a button, it should remain pressed and the others off, and when I press the second, it remains pressed and the others off.
like this :
here is the code :
<div class="button-bar">
<a class="button">First</a>
<a class="button">Second</a>
<a class="button">Third</a>
</div>
Share
edited Jan 3, 2016 at 21:16
Nikola
15k21 gold badges106 silver badges170 bronze badges
asked May 26, 2015 at 19:56
Yasser B.Yasser B.
8357 gold badges21 silver badges34 bronze badges
2
- Add an active class to the button, guy. On click, remove the active class on all the siblings and add it to the new active link. Don't overthink things. – Matthew Darnell Commented May 26, 2015 at 19:59
- @Yasser B.: did my answer prove to be useful? If yo, consider it marking as an answer, thank you. – Nikola Commented Jun 30, 2015 at 5:53
3 Answers
Reset to default 8I would use ng-class
to change the button.
Simply make a class of an active button, and then add the logic into it.
<div class="button-bar">
<a class="button" ng-click="clicked(1)" ng-class="{'active': var == 1}">First</a>
<a class="button" ng-click="clicked(2)" ng-class="{'active': var == 2}">Second</a>
<a class="button" ng-click="clicked(3)" ng-class="{'active': var == 3}">Third</a>
</div>
And in the controller:
$scope.clicked = function(num) {
$scope.var = num;
}
This would make a button 'active' in Angular.
Here is a working demo from CodePen, copy pasted below. You can run this code example directly here on StackOverflow by clicking the Run code snippet
button below.
Note: it's not a generic solution, but should get you moving in the right direction.
angular.module('mySuperApp', ['ionic'])
.controller('MyCtrl',function($scope) {
$scope.button = {};
$scope.button.first = {};
$scope.button.second = {};
$scope.button.third = {};
$scope.click = function(button){
$scope.button.first.clicked = false;
$scope.button.second.clicked = false;
$scope.button.third.clicked = false;
button.clicked = true;
};
});
<html ng-app="mySuperApp">
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<link href="//code.ionicframework./nightly/css/ionic.css" rel="stylesheet">
<script src="//code.ionicframework./nightly/js/ionic.bundle.js"></script>
</head>
<body class="padding" ng-controller="MyCtrl">
<div class="button-bar">
<a class="button" ng-model="button.first" ng-click="click(button.first)" ng-class="button.first.clicked?'button-positive':'button-energized'">First</a>
<a class="button" ng-model="button.second" ng-click="click(button.second)" ng-class="button.second.clicked?'button-positive':'button-energized'">Second</a>
<a class="button" ng-model="button.third" ng-click="click(button.third)" ng-class="button.third.clicked?'button-positive':'button-energized'">Third</a>
</div>
</body>
</html>
edit: In case you would like a leaner solution like the one Shay suggested, you can see the full implementation in Ionic on the CodePen example, pasted below. The main difference is that the clicked function can not be written as he did, and it would produce an error. Anyways, take the solution which suits you best.
angular.module('mySuperApp', ['ionic'])
.controller('MyCtrl',function($scope) {
$scope.click = function(num) {
$scope.var = num;
}
});
<html ng-app="mySuperApp">
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<link href="//code.ionicframework./nightly/css/ionic.css" rel="stylesheet">
<script src="//code.ionicframework./nightly/js/ionic.bundle.js"></script>
</head>
<body class="padding" ng-controller="MyCtrl">
<div class="button-bar">
<a class="button" ng-click="click(1)" ng-class="{'button-positive':var==1}">First</a>
<a class="button" ng-click="click(2)" ng-class="{'button-positive':var==2}">Second</a>
<a class="button" ng-click="click(3)" ng-class="{'button-positive':var==3}">Third</a>
</div>
</body>
</html>
I think this might help you:
DEMO
HTML
<a href="#" class="button" id="1">First</a>
<a href="#" class="button" id="2">Second</a>
<a href="#" class="button" id="3">Third</a>
CSS
a
{
text-decoration:none;
}
.button
{
display:inline-block;
padding:5px;
height:20px;
width:60px;
text-align:center;
background:#0CA68F;
color:white;
}
.active
{
background:#188171;
}
JS
$("a").click(function()
{
$(".button").removeClass("active");
$("#" + this.id).addClass("active");
});
本文标签: javascriptHow to use Button bar as navigation tabs ionicStack Overflow
版权声明:本文标题:javascript - How to use Button bar as navigation tabs ionic - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741534305a2383941.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论