I am creating an application using JQuery Mobile which needs to support both iOS and Android. (I am using PhoneGap). Does scrolling on long pages work by default or do I need to set up my divs to support scrolling. How is scrolling on iOS different from an android device?
Also, is there a way to make a ajax call when the user scrolls to the bottom of the page to retrieve more content?
Any help is appreciated.
1) jQuery Mobile 1.1.0 uses the browser's native scrolling so it seems natural on every supported platform.
jQuery Mobiles does however require the following pseudo-page structure:
<div data-role="page">
<div data-role="header">
...
</div>
<div data-role="content">
...
</div>
<div data-role="footer">
...
</div>
</div>
If you follow this structure, the more you add to the data-role="content" section, the longer the page will be.
2) You can set an event handler for the scroll event to detect the user scrolling and see how far down the page the user is:
//you don't want to set a bunch of AJAX requests at once,
//so set a flag so only one will go off at a time
var ajaxOK = true;
$(window).on('scroll', function () {
var yDistance = $('html, body').scrollTop();
//here you can check how far down the user is and do your AJAX call
if ((yDistance + $(window).height()) > ($.mobile.activePage.children('.ui-content').height() - 150)) {
//the user is within 150px of the bottom of the page
if (ajaxOK === true) {
ajaxOK = false;
$.ajax({ ... });
}
}
});
Then in your callback function for the AJAX call you set ajaxOK = true; so that when the user scrolls to the bottom again it will fire.
Here's a quick break-down of the if/then statement in the scroll event handler:
(yDistance + $(window).height()) > ($.mobile.activePage.children('.ui-content').height() - 150)
(yDistance + $(window).height()): the scroll-distance plus the view-port height, to find the bottom of the page's Y coordinate.
($.mobile.activePage.children('.ui-content').height() - 150): the height of the current page minus a buffer of 150px so the user can get within 150px and the AJAX call will occur
Scrolling should happen automatically if you overflow the browser window. For infinite scrolling you can try http://www.infinite-scroll.com/.
If you are using the listview for jquery mobile you may need to call the refresh event after adding more list view items to the dom to get the filtering behavior and styling to work.
Related
I am having trouble getting my range controls to function correctly in my ionic app.
I have an application where I want to use ion-slide-box to be able to slide between pages in the app. But when I place a range control on the page I can only change the value of the range control by clicking around on the control. Dragging inside the control doesn't seem to work - probably because the slide-box takes these events first.
The app is to be used on both Android and IOS.
Example codepen is here: http://codepen.io/flemmingdjensen/pen/emzXmB
<ion-slide-box>
<ion-slide>
<h1>Test of ion-slide-box and range control</h1>
min <input type="range" style="width:80%"> max
<br><br>
page 1
</ion-slide>
<ion-slide>
<br><br>
page 2
</ion-slide>
</ion-slide-box>
Try:
<input type="range" on-touch="enableSlide(false)" on-release="enableSlide(true)" />
And
$scope.enableSlide = (enable) => { $ionicSlideBoxDelegate.enableSlide(enable); };
I'm using JQuery Mobile + PhoneGap for my mobile app.
I have some links and buttons with onClick event attached
<li onclick="func();"></li>
I having problems when user double tap, can I somehow disabled it for current DIV ?
Give a id to your <li id="someid">
Then
$('#someid').dblclick(function(e){
e.preventDefault();
})
To prevent double click on multiple tags, just use a class on those li tags, and use the same code
$('.someClass').dblclick(function(e){
e.preventDefault();
});
I am trying to develop a shopping cart system using kendo-ui mobile and phonegap. First I am listing all the items in a list view. In each listview item, there will be one plus button, minus button and a label.I am using this combination for selecting the quantity of items.So, if we click plus button, the label value should be 0+1=> 1 and when we click minus, it should be like 1-1=>0 .To change the value of label when clicking button, I am passing the id of label to change the corresponding label value. But I am not able to pass the id form html to javascript, like I do in web development. Here is my code,
My listview item template,
<script type="text/x-kendo-tmpl" id="endless-scrolling-template">
<div class="product">
<img src="images/image.jpg" alt="#=ProductName# image" class="pullImage"/>
<h3>#:ProductName#</h3>
<p>$#:kendo.toString(UnitPrice, "c")#</p>
<a id="minus" data-role="button" data-click="minus(#:ProductID#)" >-</a>
<label id=#:ProductID#>0</label>
<a id="plus" data-role="button" data-click="plus(#:ProductID#)" data-name="plus">+</a>
<a id="loginButton" data-role="button" data-click="login">Add to Cart</a>
<div class="console"></div>
</div>
and my javascript functions,
<script>
function plus(itemid) {
var quantity=document.getElementById(itemid).innerHTML;
document.getElementById(itemid).textContent = parseInt(quantity)+1;
}
function minus(itemid) {
var quantity=document.getElementById(itemid).innerHTML;
document.getElementById(itemid).textContent = parseInt(quantity)-1;
}
</script>
Can anyone please tell me what Iam doing wrong here? Or can you provide an alternate solution?
When using Kendo, you can use Kendo MVVM. When JS objects are wired up in views using Kendo MVVM, when the value of the input elements change, the value of the JS objects reflects the change automatically. So what you need to do is to create a JS model for your view and set it as your view's model using data-model="yourModel". See this link: http://docs.kendoui.com/getting-started/mobile/mvvm
In your scenario here I think this link will help you: http://demos.kendoui.com/web/mvvm/source.html
This behavior is explained in the Kendo mobile book I wrote and you can see the checkout screen of the sample application built for the book here: http://movies.kendomobilebook.com/
I have a main navbar, which loads contents - using .load() - to a div data-role=content according to whats selected,
so i use the TapHandler event on the navbar, to load the content like:
$("#mainc").load(target, function() {
$("#index").trigger("pagecreate"); //#index is the page id
});
Div where content is being loaded:
<div data-role="content" id="mainc">
</div>
Whats being loaded: ( the second navbar )
<div id="profile_navbar" data-role="navbar">
<ul>
<li>My Profile</li>
<li>Events</li>
<li>Settings</li>
</ul>
</div>
And the problem is, the first click, evrything goes perfect, then, it starts duplicating some borders on and on, the more i change tabs, the more borders it gain,
i dont use .trigger('create') there, because it doesnt get the loaded content styled with the jqm css.
Images:
What is going on here?
Thanks.
After some hours of struggle, I found that the problem was not with .trigger(“pagecreate”) but with .load()
So i came up with this: (dont ask me why, all I know is that it works this way)
$.get(target, function(data) {
$('#mainc').html(data);
$('#mainc').trigger("create");
});
instead of:
$("#mainc").load(target, function() {
$('#mainc').trigger("create");
});
I am currently exploring jQuery Mobile's functionalities. I am quite intrigued by the way Google Play Store handles horizontal sliding, hence, when the user slide taps to the right, the view should slide to the next page and when the user slide taps to the left the view should scroll to the previous page, if any. I know this can be done using native jQuery but I'm not yet familiar with events on mobile devices and I'm sure there is already a built-in functionality for this.
I'd like to try this first with Android devices and if possible with iPad and iPhone. Can anybody guide me on ways to accomplish this?
BTW, I'm not talking about browser history here, probably just some div, pages, if possible.
It can be done but you will be sadly disappointed.
It can be achieved like this:
Multiple page's inside one single HTML. Every page will have swipeleft and swipe right binded to it. When event is triggered changePage() function will make a transition to previous/next page. This sounds excellent and works just fine on desktop browser but fails miserable when executed with phonegap on android phones. Transitions are still a huge problem on android phones, iOS fares better but not to much.
Something like this:
$('#page-two').on('#page-two', 'swipeleft', function () {
//next page
$.mobile.changePage($('#page-three'));
}).on('#page-two', 'swiperight', function () {
//prev page
$.mobile.changePage($('#page-one'), { reverse : true });
});
Swipe events are supported with jQuery Mobile so no need for 3rd party plugins.
Use a jQuery Mobile carousel plugin like this example: http://jsfiddle.net/blackdynamo/yxhzU/
Original plugin: https://github.com/blackdynamo/jQuery-Mobile-Carousel
Unlike page transitions this plugin will give you much better feeling on mobile phones.
What ever path you choose android tab look will be achieved with navbar inside a second header:
<div data-theme="a" data-role="header">
<h3>
First Page
</h3>
Next
</div>
<div data-theme="a" data-role="header">
<div data-role="navbar">
<ul>
<li>Page One</li>
<li>Page Two</li>
<li>Page Three</li>
</ul>
</div><!-- /navbar -->
</div>
Solution #1 -- This should be the easy way to do it:
<script>
$(document).delegate("#homepage", 'pageinit', function (evt) {
$(this).bind("swipeleft", function (e) {
$.mobile.changePage("#anotherpage", {
transition : 'slide'
});
});
}).delegate("#anotherpage", 'pageinit', function (evt) {
$(this).bind("swiperight", function (e) {
$.mobile.changePage("#homepage", {
transition : 'slide',
reverse : true
});
});
});
</script>
Solution #2 -- This one is even much simpler:
$('#homepage').bind('swipeleft', function() {
$.mobile.changePage('#anotherpage', {transition: 'slide', reverse: false});
});
$('#anotherpage').bind('swiperight', function() {
$.mobile.changePage('#homepage', {transition: 'slide', reverse: true});
});