jQuery mobile pageshow getting element position - jquery-mobile

In my jQuery Mobile app I want to "slide" the UI to a certain position when a page is displayed but it seems that when I get the offset object of the element on the page I want to slide to in the pageshow event, that the y coordinate is always 1.
My code looks something like this:
$(document).on('pageshow', function() {
var offset = $('#my-element').offset();
// offset.top is always 1
} );
What would be the best way to overcome this issue?

This is a wild guess but I know only one case where this can happen.
It can happen if element you are searching for also exist in some other page, mainly if it will be find in the DOM before element you are currently searching. Because that page is not visible it don't have height.
This should help, change
var offset = $('#my-element').offset();
to this:
var offset = $.mobile.activePage.find('#my-element').offset();
$.mobile.activePage is a selector that will give you access to currently active page. It will disregard everything else.

Related

Phonegap app on iOS - page scrolls on text input focus

Today I'm facing "a typical" problem on iOS with a page scrolling when a user selects an input text. The point is that I'm using a scrollable page including a lot of texts and I need this. So I can't use a solution that disables scolling by preventing default, minimizing the view etc..
I basically need to open a software keyboard without scrolling the page.
I also need to find a solution that will be suitable for my users - no screen blinking, etc... After some research I finally managed to find a working solution.
Please see my answer below.
The idea is to return the page to its original position as soon as possible and thus prevent the iOS scrolling animation.
You simply add a focus handler to your input text field and in this handler you firstly read the window.scrollTop property and than set it back with a delay of 0 ms.
Here is my code (using jQuery):
$("#myinput").on("focus", function() {
var scrollTop = $(window).scrollTop();
setTimeout(function() {
$(window).scrollTop = scrollTop;
}, 0);
});
I hope there is no bug in this - actually I'm using TypeScript so if you want to see my original code here it is:
this._inputText.on("focus", () => {
var jQueryWin:JQuery = <JQuery>$(window);
var scrollTop:number = jQueryWin.scrollTop();
setTimeout(() => {
jQueryWin.scrollTop(scrollTop);
}, 0);
});
OK - I hope this helps you guys to better control an unwanted page scrolling when dealing with input elements.

Jquery Mobile 1.3 slider conflicts with panel

I am trying to set up a JQuery Mobile 1.3 site that uses a panel and a slider.
Problem is, that using the slider triggers the panel, which opens on a "swiperight" event, as I am moving the slider to the right. The slider will be for pagination, the panel for a menu.
Code here:
http://jsfiddle.net/kMARn/1/
Move the slider to the right and the panel will open.
I have tried using the .not() selector for the panel to not react on the slider:
$(document).not("#slider").on("swiperight", function(event, ui) {
$("#myPanel").panel("open");
});
But it won't work, the panel opens when i move the slider to the right. Tried a bunch of variants too, but I'm lost...
Any ideas?
Thanks!
A bit late to the party, but you can disable swipe-to-close by setting the data-swipe-close attribute to "false" on the panel div.
http://jquerymobile.com/demos/1.3.0-beta.1/docs/panels/options.html
In my case I used this simple code, without data-swipe-close = "false" in panel.
Keeping panel close with swipe right, outside of the slider.
$('#panel').find('#slider')
.on('slidestop',function(e,ui) {
var value = e.target.value;
//...operations with slider value...
})
.parent().on('swiperight',function(e,ui) {
e.stopPropagation(); //block panel close
})
From the 1.3.0b1 Docs for Swipe:
"Triggers when a horizontal drag of 30px or more (and less than 75px
vertically) occurs within 1 second duration"
This applies to and can be configured for swiperight too. You can make the slider small in length and this would ensure that both the slider event stop and the swipe are not triggered at the same time, yet that may not be practical for all scenarios.
What might be better is to bind the swipe right to a DIV or section of the page. By this, I mean if you have a 75 px div box on the left hand side of the display, and when a swipe event occurred within that div, it could trigger the menu.
I feel the logic here might be better controlled by a button, much like used in the Facebook App to display there slide out menu. In the Dolphin browser on Android, this type of event also triggers a bookmark menu, so if a page has a swiperight event and trigger it, I sometimes get both the event and the bookmark menu from the App. Annoying!
I did fork your jsfiddle and will play with it more (http://jsfiddle.net/Twisty/Hg2pw/). FYI, they have JQM 1.3.0b1 in their available frameworks so you don't have to link it in your HTML. If I find some more info, I will comment here.
The following solution is more a workaround. It should be relatively reliable though.
$(document).ready( function () {
var menu_available = true;
$(document).on("swiperight", function(event, ui) {
if (menu_available) $("#myPanel").panel("open");
});
$("#slider").on("slidestop", function( event, ui ) {
menu_available = false;
window.setTimeout(function() {menu_available= true;},250);
});
});
The variable menu_available is false for a 250 milliseconds right after the slide stops. The window.setTimeout block will reset the variable so that the menu is available again.
This is a stupid workaround, but jQuerys function event.stopEventPropagation(), which IMHO would be the correct way to go, didn't work.

How do i automatically scroll a page when it loads in jQuery mobile

I want to automatically scroll to a particular div which is at the bottom of the page my [data-role="page"], but it's not working. The scroll bar still remains at the top but I want it to scroll down when the page loads. Let me know if you need better explanation. Thanks in advance.
You can bind to the pageshow event and then use the $.mobile.silentScroll() method to scroll the desired element into view. Here is an example:
$(document).delegate('.ui-page', 'pageshow', function () {
//get the offset of the element
var offset = $(this).find('[some-element]').offset().top;
//now scroll to the element
setTimeout(function () {
$.mobile.silentScroll(offset);
}, 0);
});
Here is a demo: http://jsfiddle.net/JNSRn/
The setTimeout allows the scroll to run after everything else that has been queued to run actually runs.
You can change the .ui-page selector to an ID or class to only run this code on a specific page or on a specific set of pages, currently it will run the event handler when any jQuery Mobile pseudo-page is shown.
Docs for $.mobile.silentScroll(): http://jquerymobile.com/demos/1.1.1/docs/api/methods.html (Bottom of page)
You can try creating a anchor tag and clicking it (you don't even need to attach it to the DOM, for example
var a = $('<a />').attr('href', '#myParticularDiv').click();

jQuery Mobile: scrollview and swipe events

I have a basic JQM page which displays a left side sliding menu when swiping (like the facebook mobile app). It worked fine until I started using scrollview (to properly keep the header fixed). The swipe event is not triggered when I swipe over my page content (it still works if I swipe on the header).
$('.ui-page-active').live("swiperight", function() {
if (!menuStatus) {
showMenu();
}
});
Does anyone has any idea on how to make it work?
Cheers!
I think you might find that the event is consumed in the scrollview control. You can override the javascript handler against that control to allow it to keep bubbling.
To override the function you can use this technique: Overriding a JavaScript function while referencing the original
Having a quick look at the file, it looks like this method could hold clues as to what you want:
_handleDragMove: function(e, ex, ey)
specifically:
var svdir = this.options.direction;
if (!this._directionLock)
I was using the version of scrollview shown here: http://jquerymobile.com/test/experiments/scrollview/scrollview-nested.html

jQuery droppable - how to react on hover after a short period of time?

I am using jQuery draggable/droppable to drag (divs) from one list to another. Both lists have a fixed number of list items, and the list items themselves and can either be empty or already contain a div.
I am dragging a div to a list item. If the list item already contains a div then I would like that div to be pushed down to the list item below it - so as to make way for the div I am dragging. I can do this with the "over" event in droppable. But I would like this to happen only after the user has paused there for a cetain period of time, say 0.5 second. How can I do this?
Also, if the user decides not to drop then I would like the displaced item to come back to its original position. Again, what would be the easiest of accomplishing that?
Thanks for your help
William
I just happen to be doing something similar. In my case, I am using the treeTable plugin. I want to expand a collapsed tree if it is hovered for some amount of time. Here is what I have so far. I just happen to be working on this today. Note the use of a timeout to delay the operation.
var timeout = null;
var clear = function() {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
}
$(".selector").droppable({
over: function(e, ui) {
if (! $(this).is("expanded")) {
clear();
timeout = setTimeout(function() {
$(this).expand();
timeout = null;
}, 1000);
}
}
, out: clear
, drop: function(e, ui) {
clear();
...
}
});
I'm still having a problem with this. After one branch gets expanded, the rows below it get pushed down to make room for the expanded branch. I haven't dropped yet and so I go hover over the first new element in the expanded branch. The problem is that this element does not get the hoverClass, the first element that got pushed down below the new branch gets it. It is like jquery doesn't figure out that it got moved down. I imagine that you are likely to encounter this problem given what you've described.
I was on jquery-1.4.2 but then I updated to jquery-1.6.4 to see if that would fix the problem. Both versions exhibit the same behavior. Also, I tested this in Chrome 14.0.835.202, Opera 11.52, Firefox 3.6.23 and IE7.0.5730.13. It happens the same way in all of them. So, either jquery has a bug or I've done something to cause this.
About your second question. I'm not sure I completely understand what you're going after. Are you looking for the

Resources