I have a page with several sections of significantly varying length within a jQuery UI Accordion. If I open a new section (which collapses one of the longer sections above), I'm left at the bottom of the page. Because the sections are of significantly different heights, I can't use the autoheight feature without it looking very strange. Is there any way to use something like scrollto to automatically go to the top of the section I've just expanded?
You can bind a function to the accordionchange event and use jQuery scrollTop():
JavaScript
$(function () {
$("#accordion").accordion({
autoHeight: false,
header: "h3"
});
$('#accordion').bind('accordionchange', function (event, ui) {
$(window).scrollTop(ui.newHeader.offset().top);
});
});
HTML
<div id="accordion">
<div id="accordion-one">
<h3>First</h3>
<div>Some lengthy text</div>
</div>
<div id="accordion-two">
<h3>Second</h3>
<div>Less lengthy text</div>
</div>
<div id="accordion-three">
<h3>Third</h3>
<div>Other text</div>
</div>
</div>
I tested this in FF8.
Links
Accordion change event
jQuery scrollTop()
Related
using Jquery UI, I'm trying to drag elements and drop them inside bootstrap columns. A simple idea but doesn't work, as you may try yourself:
http://jsfiddle.net/r4rp93ac/1/
It only drops inside the first bootstrap column
I found related questions:
Bootstrap 3 column class interfering with jquery-ui droppable div
jQuery UI Draggable with Bootstrap layout
and tried to add
.ui-draggable-handle {
z-index: 1;
}
.ui-draggable-dragging {
z-index: 10000!important
}
to css, but doesn't help.
Id should be unique, so if you wan two droppable zones, use class instead:
<div id="droppable2" class="droppable col-xs-3">
<p>Drop
<br>here</p>
</div>
<div id="droppable2" class="droppable col-xs-3">
<p>Drop
<br>here</p>
</div>
And
$(".droppable").droppable({
drop: function (event, ui) {
$(this).find("p").html("Dropped!");
}
});
Working demo
I want to set a default tab in jQuery Mobile.
My source code:
<div data-role="tabs" id="tabs">
<div data-role="navbar">
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
</div>
<div id="one" class="ui-body-d ui-content">
<h1>First tab contents</h1>
</div>
<div id="two">
<ul data-role="listview" data-inset="true">
<li>Acura</li>
<li>Audi</li>
<li>BMW</li>
<li>Cadillac</li>
<li>Ferrari</li>
</ul>
</div>
</div>
$("#tabs").tabs({ active: 0 });
It worked but has no background color, because the first tab is not actually clicked.
I want to set default tab with background when I login in.
No background color demo
To set the active tab, try:
$( "#tabs" ).tabs("option", "active", 1);
Here is a working DEMO
UPDATE: the blue background on the tab button comes from the class ui-btn-active. Either add this class to the button, or instead of setting the active tab, trigger the click event on the appropriate button: http://jsfiddle.net/ezanker/c29gd4h6/1/
I have added class="ui-btn-active" in first tab-li
<div data-role="navbar">
<ul>
<li>One</li>
<li>Two</li>
</ul>
</div><!-- /navbar -->
If not work then try below It working for me when page load only I mean refresh page in browser
Put following code in jquerymobile pagecreate event that will make every first tab selected
$('[data-role="tabs"] a:first').each(function() {
$(this).click();
});
I had put above lines into pagecreate event
$(document).on("pagecreate", "#homepage", function(event) {
$('[data-role="tabs"] a:first').each(function() {
$(this).click();
});
});
In above line #homepage is my page id and pagecreate is event of jquery-mobile that fire when page load/init
HTML
<div data-role="tabs" id="tabs">
<div data-role="navbar">
<ul>
<li>Default</li>
JS
$(document).on("pagecreate", function (event) {
$('#tab-one').trigger('click');
});
I found the existing mentioned solutions had issues:
the one by #ezanker showed the content but didn't set the tab button as active
the one by #Devendra Chhaiya clicked any first link that the tab content contained also! :-)
I also found that the tab wasn't remaining selected when coming back to that page, so I used this solution to fix that too.
there was also an issue with pages loading into tab content, so I had to fix that too
in addition (yes, there's more :-) I wanted to selected a specific tab based on the requirements of that specific page, so I added a class select-this-tab on page generation from the server.
So I modified the one by #Devendra Chhaiya to click only the actual tab button (and not any tab content, keep the tab selected, and also prevent content loading into the tab area. I had to move the trigger from pagecreate to pageinit for it to work:
/*
* https://stackoverflow.com/questions/13837304/jquery-ui-non-ajax-tab-loading-whole-website-into-itself/17384908#17384908
*/
jQuery(function () {
jQuery('base').remove();
jQuery("#tabs").tabs();
});
$(document).on('pageinit', function () {
console.log('pageinit');
/*
* Ensure the correct tab of a set is selected (and only once)
* https://stackoverflow.com/questions/25336233/jquery-mobile-default-tab/64080164#64080164
*/
$('[data-role="tabs"] [data-role="navbar"] ul li.select-this-tab').each(function () {
console.log('Clicking tab');
$(this).removeClass("select-this-tab").find("a").click();
});
/*
* Ensure a tab REMAINS selected when coming back to that page.
* https://stackoverflow.com/questions/16752704/tab-active-state-in-jquery-mobile/23725612#23725612
*/
$('div[data-role="tabs"] [data-role="navbar"] a').click(function (e) {
e.preventDefault();
$('div[data-role="tabs"] [data-role="navbar"] .ui-btn-active').removeClass('ui-btn-active ui-state-persist');
$(this).addClass('ui-btn-active ui-state-persist');
});
You can remove the console.log lines which you can just use to prove the code actually fires.
I am using jQuery UI to make selections. I'm having an ul-list that I made selectable. The li-items contains icons and texts. It seems that the selectable comment not only makes the li-items selectable, but also the elements in the li-items. This gives some unexpected results.
I tried to make an example in jsFiddle: http://jsfiddle.net/eJSGU/
If you click several times on the edges of the icon, you will see that there is sometimes something selected that is bigger than the li-block.
<li class="ui-widget-content">
<div class="img"><img src="http://bib.arts.kuleuven.be/bibliotheek/images/icon_facebook.jpg"></div>
<div class="lbl">Item 1<div>
</li>
Anyone an idea how I can avoid this?
I suggest to use the filter option of the selectable. In your case you want only the li elements to be selectable so you set filter: $('selector').children()'.
<script>
$(function() {
$( "#selectable li" ).selectable({
filter: $('#selectable').children('li')
});
});
</script>
Here is an updated fiddle.
I thought the basic syntax for a slider was:
<div id="slider"></div>
$("#slider").slider()
But that doesn't make a visible slider. Inspecting the div element, I see that some stylings were applied, but the slider isn't visible.
Here's a fiddle: http://jsfiddle.net/yqNcn/
You have not included the CSS for jQuery UI so classes are applied but styles are not (there are actually none to apply). Here's an edited fiddle that includes the styles (look at the Resources panel); the slider is visible as it should be.
First Import your javascript Files* jquery.s and import Jquery UI widget Jquery-ui.js
Then
Put Your Css Div Tag with id <div id="slider"></div> this Example
<div id="slider"></div>
then Call Jquery slider function....
<script>
$(function() {
$( "#slider" ).slider();
});
</script>
I'm using jQuery UI's tabs to divide content on my page. I have a 'link bar' I would like to have hang at the bottom of each tab. (The tab text will change but generally they will navigate the user left or right through tabs.)
Hosting the #linkBar div inside the first tab makes it 'look' right, inside Themeroller's border. Putting it just outside the 'parent tab' div places the links below the theme's border. I've tried creating a spacer div but it just pushes #linkBar down further.
Of course when the user switches to another tab, the link bar goes away. How is ownership of elements organized between tabs? Should I dynamically destroy the #linkBar div on the tab being navigated away from and rebuild it in the tab being navigated to? Or is there a better way to move it between them, or just manage visibility?
I would like to have the link bar follow the content on each tab as a footer, 'floating' one or two lines below the last content of each tab (rather than having it in a fixed position relative to the tab bar).
Ok ... It was simply adding the jQuery UI classes to the linkBar. Check out my working jsFiddle demo:
I moved the linkBar div out of the tabOne div and put it at the bottom of the tabs div:
<div id="container">
<div id="title">
<h1>title bar</h1>
</div>
<div id="tabs">
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
<div id="tabone">
content goes here
<br><br><br><br>more stuff<br><br><br>more stuff<br><br>
</div>
<div id="tabtwo">
content goes here...
</div>
<div id="tabthree">
content goes here...
</div>
<div id="linkBar">
<span id="leftLink"><< left link</span>
<span id="rightLink">right link >></span>
</div>
</div>
</div>
I slightly altered the linkBar style by giving it a top and bottom margin as well as hiding it by default:
#linkBar {
display: none;
margin: 10px auto;
}
Then I simply added the jQuery UI classes to the $linkBar. I slightly altered the jQuery to be more readable:
$("#accordion").accordion({ header: "h3" });
var $tabs = $("#tabs"),
$linkBar = $("#linkBar");
$linkBar.addClass("ui-tabs-panel ui-widget-content ui-corner-bottom");
$linkBar.show();
$tabs.tabs();
$('#title').click(function() {
$tabs.tabs('select', 0);
return false;
});
Note: You could just add class="ui-tabs-panel ui-widget-content ui-corner-bottom" to the linkBar div and be done with it. But, I think I like it better managed in the JS.