switch
The <switch> component is used to enable or disable a function.
Required Permissions
None
Child Component
None
Attributes
In addition to the attributes in Universal Attributes, the following attributes are supported.
Styles
In addition to the styles in Universal Styles, the following styles are supported.
Distance between the two sides of the longest text in texton and textoff and the border of the slider. |
||||
Font size. This attribute is available only when texton and textoff are set. |
||||
Whether the font size changes with the system's font size settings. |
||||
Font style. This attribute is available only when texton and textoff are set. For details, see font-style of the text component. |
||||
Font weight. This attribute is available only when texton and textoff are set. For details, see font-weight of the text component. |
||||
Font family, in which fonts are separated by commas (,). Each font is set using a font name or font family name. The first font in the family or the font specified by Custom Font Styles is used for the text. This attribute is available only when texton and textoff are set. |
Events
In addition to the events in Universal Events, the following events are supported.
Method
Methods in Universal Methods are supported.
Example Code
<!-- xxx.hml -->
<div class="container">
<switch showtext="true" texton="On" textoff="Off" checked="true" @change="switchChange">
</switch>
</div>
/* xxx.css */
.container {
display: flex;
justify-content: center;
align-items: center;
}
switch{
texton-color:#002aff;
textoff-color:silver;
text-padding:20px;
}
// xxx.js
import prompt from '@system.prompt';
export default {
data: {
title: 'World'
},
switchChange(e){
console.log(e.checked);
if(e.checked){
prompt.showToast({
message: "Switch on."
});
}else{
prompt.showToast({
message: "Switch off."
});
}
}
}