I use Ionic 3. How to make my button change the image of my background by clicking?
This is my button.
<button id="transparent" ion-button round color="light"> </button>
And this is the background that is already installed on my page.
#back {
background-size: 100%;
background-repeat: no-repeat;
background-size: cover;
width: 105vw;
height: 100vh;
margin-left: -10%;
background-attachment: fixed;
margin-top: -10%;
background-position: center center;
}
.ion-content {
background: url('myurl');
}
<div id="back" class="ion-content"> </div>
Use
[style.background]
:(I created demo:https://stackblitz.com/edit/ionic-mgbhjq?file=pages%2Fhome%2Fhome.html)
TS(component)
TO your comment:
use
attr.id
:TS(component)
Ionic uses Angular under the hood. So best way to handle the event is in controller of your view. So in your 'YourController.ts' file you need to write handler method like this:
Then you can use this event in your button like this:
Above is simple angular way of binding event to handler method. Now we can use Angular attribute binding to bind style attribute to 'image' property like this:
So basically when user will click your button then following things will happen:
YourController.ts
will be called.image
to new value.To all those who are still wondering why Angular is being used here can watch this: Ionic Intro and Crash course