How to change Background by clicking a button? - html

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)
<div id="back" class="ion-content" [style.background]="'url('+image+')'"></div>
<button id="transparent" ion-button round color="light" (click)="changeImage()"> </button>
TS(component)
image:any;
ngOnInit(){
this.image ="your first url";
}
changeImage(){
this.image="your second url";
}
TO your comment:
integrate an id for my second background
use attr.id:
<div attr.id="idParam" class="ion-content" [style.background]="'url('+image+')'"></div>
TS(component)
image:any;
idParam:string="back";
ngOnInit(){
this.image ="your first url";
}
changeImage(){
this.image="your second url";
this.idParam="secondBack";
}

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:
changeBackground() {
this.image = '<url_of_new_image>';
}
Then you can use this event in your button like this:
<button id="transparent" ion-button round color="light" (click)="changeBackground()"> </button>
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:
<div id="back" class="ion-content" [style.background]="'url('+image+')'"></div>
So basically when user will click your button then following things will happen:
changeBackground() method of YourController.ts will be called.
Here we are setting property image to new value.
So DOM manipulation of Angular will change div background to this new value.
New image replaces existing image.
To all those who are still wondering why Angular is being used here can watch this:
Ionic Intro and Crash course

Related

Cannot make an image button on WeChat mini-program

I want to make buttons with image icons on WeChat mini-program. For example, play/pause, next, previous buttons in media player.
I tried as html:
<button>
<image src="../image/play.svg"></image>
</button>
But I cannot see any image on button.
How to make an image button?
For image buttons, just use <image> tag.
For example:
<image src="../image/play.svg" bindTap="play()"></image>
And set some button-like styles using WXSS.
image {
background: lightblue;
}
image:hover {
background: lightgrey;
}
Of course, your icon image should have transparent background.
I have achieved it with
<button bindtap = "mytap">
<image src = '../../image/search-icon.png' style=" width: 35rpx;height: 35rpx;"></image>
Search
</button>
You can also put inside the button only the image element
<button bindtap = "mytap">
<image src = '../../image/search-icon.png' style=" width: 35rpx;height: 35rpx;"> </image>
</button>
In javascript or typescript files put:
Page({
//...
mytap(){
console.log("clic")
}
})
For more details you can consult: Implement picture button in WeChat Mini Program

Add element <button> on ng2-pdf-viewer Angular

I need to display PDF file on my component using ng2-pdf-viewer, but one of its requirement,I need to add button downloadand it must overlapping PDF file, try to find any reference regarding this but none found, this is what I had tried,
component.html
<button (click)="toggle()">VIEW RECEIPT</button>
<div style="height:715px">
<pdf-viewer *ngIf="isHideReceipt" [autoresize]="true" [src]="pdfSrc" [original-size]="false"
[render-text]='false' [show-all]="false" style="display: block;position: relative"
[fit-to-page]="true">
</pdf-viewer>
<button (click)="download()">Download PDF</button>
</div>
component.ts
pdfSrc = '../../assets/pdf/bla3.pdf';
toggle() {
this.isHideReceipt = !this.isHideReceipt;
}
download() {
const blob = this.pdfSrc;
saveAs(blob, 'test1.pdf');
}
as per-requirement (button download overlapping pdf), Im trying using CSS like z-index but none work, it is possible ?
link to official ng2-pdf-viewer
Set the button element to have an absolute position and it's parent container to have a relative position. That way you'll be able to overlap onto the pdf viewer:
<button (click)="toggle()">VIEW RECEIPT</button>
<div style="position: relative; height:715px;">
<pdf-viewer *ngIf="isHideReceipt" [autoresize]="true" [src]="pdfSrc" [original-size]="false"
[render-text]='false' [show-all]="false" style="display: block;position: relative"
[fit-to-page]="true">
</pdf-viewer>
<button style="position: absolute; right: 10px; bottom: 10px;" (click)="download()">Download PDF</button>
</div>

Ionic v3 align button and icon

When creating a button on the ionic creator(v3) I can create a button like this
But when I export the template it renders like so:
Here is my HTML:
<button ion-button icon-end block color="energized">
<span>Edit</span>
<ion-icon name="create"></ion-icon>
</button>
How do I correctly align the button and icon so that the icon is positioned at the end and the button text is centered. Is there a built in way I can do this using ionic directives? Or do I have to create custom CSS to achieve this?
From css, you can add
button ion-icon {
position: absolute;
right: 20px;
}
Selector can be button[icon-end][block] ion-icon{...}

ng-click= checkPhoneError() does not get called

I am newbie and inherited this code. When I click on the Call button checkPhoneError() does not get called. What is missing here? Thanks
<ion-nav-title>Status: {{name}}
<a href="{{::getPhoneNumber()}}" class="blue" ng-show="::showCallButton">
<div id="callArea"
class="button"
ng-class="::getCallButtonColor()">
Call
</div>
</a>
<div id="buttonOverlay" ng-click="checkPhoneError()"></div>
</ion-nav-title>
This is the Controller Code. So when the call button is clicked I hope to break to this funciton:
$scope.checkPhoneError = function () {
if (phoneNumberError) {
displayError();
$fileLogger.log("error", "There is no phone number for " + analysis.name);
}
else if ($scope.alertLevel != '') {
PaymentService.call(analysis.name, $scope.alertLevel);
}
};
Sorry I should have added this too, I did not realize what all was needed for people to get full context. Here is my size of the button from the CSS file. Yes I can click and the control goes to the phone and I can make a call. What I want is to know that the phone call happened. BTW this a cordova/ionic app running on Android phone:
#buttonOverlay {
position: absolute;
height: 100%;
width: 60px;
right: 0;
top: 0;
padding-top: 3px;
opacity: 0;
}
I Googled and got some hint and tried this. Now I can break into my function but the call button does not show :(. However when I hover there the cursor becomes live(becomes a hand) and I can click the execution breaks into my function, now only if I can see the call button.
<ion-nav-title>Status: {{name}}
<div id="buttonOverlay" ng-click="checkPhoneError()">
<a href="{{::getPhoneNumber()}}" class="blue" ng-show="::showCallButton">
<div id="callArea"
class="button"
ng-class="::getCallButtonColor()">
Call
</div>
</a>
</div>
</ion-nav-title>
Finally I hit the solution thanks to Google!! Here is the working code. So what was happening was a-tag will set the link and make it automatically clickable. So the default ng-click was masking my ng-click. All I had to do was to override the default ng-click with my own.
<ion-nav-title>Status: {{name}}
<a href="{{::getPhoneNumber()}}" class="blue" ng-click="checkPhoneError()" ng-show="::showCallButton">
<div id="callArea"
class="button"
ng-class="::getCallButtonColor()">
Call
</div>
</a>
<div id="buttonOverlay" ></div>
</ion-nav-title>
Finally I hit the solution thanks to Google!! Here is the working code. So what was happening was a-tag will set the link and make it automatically clickable. So the default ng-click was masking my ng-click. All I had to do was to override the default ng-click with my own.
<ion-nav-title>Status: {{name}}
<a href="{{::getPhoneNumber()}}" class="blue" ng-click="checkPhoneError()" ng-show="::showCallButton">
<div id="callArea"
class="button"
ng-class="::getCallButtonColor()">
Call
</div>
</a>
<div id="buttonOverlay" ></div>
</ion-nav-title>

ionic2 - ion-content to the top of the screen

I am developing a mobile app that deal with a lot of images. My client want the app main page to have a full screen background image edge to edge. But all I can achieve so far is like the image below:
I just not sure how to get rid of the white gap on top. Here are my code:
home.html:
<!--<ion-header> <ion-navbar>
<ion-title>
Ionic Blank
</ion-title> </ion-navbar> </ion-header>-->
<ion-content> <div class="gmHomeTopScreen">
<p *ngIf="user">
<i>Welcome, {{user}}</i>
<p>
<button class="button button-clear button-positive" (click)="openModal({charNum: 0})">
Gollum
</button> </div> </ion-content>
home.scss:
page-home {
.gmHomeTopScreen{
width: 100%;
height: 100%;
background-color: #999;
}
}
I read the ionicframework docs about content, they did have a instance member called contentTop. But I am not sure how to use it.
How is it possible for me to set every page with the ion-content on top of the page? I know it has to do with app.scss but I am not sure how to do that.
Try adding fullscreen attribute to ion-content