Showing posts with label jQueryUI. Show all posts
Showing posts with label jQueryUI. Show all posts

Thursday, January 30, 2014

Add Anchors to jQuery UI Accordion Headers

If you want jQuery UI's accordion (version 1.10.0+) to be able to open the desired section based on the window hash, you are stuck with using a hash that looks something like this "#ui-accordion-accordion-header-1", and well, the panel won't open. It requires some extra coding.

With this code you can add a link within the header which opens the panel and updates the hash. It also opens the panel based on the hash after the accordion initializes.
I originally answered this code on Stackoverflow, but thought it would be nice to have a write up with demo.
var hashId = 0,
    $accordion = $('#accordion');
if (window.location.hash) {
    $accordion.children('h3').each(function (i) {
        var txt = this.textContent.toLowerCase().replace(/\s+/g, '_');
        if ( txt === window.location.hash.slice(1) ) {
            hashId = i;
        }
    });
}
This above first block of code sets a default hash id (zero-based index of the currently open header), then looks within each accordion header's text and checks if it matches the current window hash. The line with this.textContent (may not work in older browsers, so change it to $(this).text() as needed), gets the header text and replaces any spaces, tabs, etc that don't work within an element ID. If the header text is really long, contains punctuation like commas, exclamation points, etc, then you may need to truncate the text with something like this:
var txt = $(this).text()
    .toLowerCase()
    .replace(/[^a-z\s]/g,'')
    .replace(/\s+/g, '_')
    .substring(0,10)
This code replaces any text that isn't a letter in the alphabet, or a space, replaces spaces with an underscore, then saves the first 10 characters.
Then it compares it to the window hash. The .slice(1) removes the hash ("#") before comparing it to the accordion header text.
The hash id is then set using the header index.

Now we can initialize the accordion...
$accordion.accordion({
    active: hashId,
    animate: false,
    heightStyle: 'content',
    collapsible: true,
    create: function (event, ui) {
        $accordion.children('h3').each(function (i) {
            // set id here because jQuery UI sets them as "ui-accordion-#-header-#"
            this.id = this.textContent.toLowerCase().replace(/\s+/g, '_');
            // add the anchor
            $(this).before('');
        });
        $accordion.find('.accordion-link').click(function () {
            // the active option requires a numeric value (not a string, e.g. "1")
            $accordion.accordion( "option", "active", $(this).data('index') );

            // uncomment out the return false below to prevent the header jump
            // return false;
        });
    }
});

Set the active option to initialize the accordion with the hash tag matching section open, it uses a zero-based index.

The animate option is set to false because when the user clicks on a link, the page jumps to the section... then the animation opens the panel. It sometimes animates so the entire page scrolls up and the section header is no longer at the top of the page.

The heightStyle and collapsible options are set to my personal preference, change them as desired.

Now the create option needs to contain code to add the links and make them clickable. It cycles through all of the section headers, replaces the id to match the header text - use the code that was used to match the text in the first block of code, so the code will compare the text parsed in the same way - then add the link before the header. If the link is added after the header, the accordion messes up because it is set to look for the element (a <div>) immediately following the header. The link contains a "data-index" attribute which contains the header zero-based index used to set the active accordion panel.

And finally the code to make the link clickable. After setting the "active" accordion option, you can chose to return false, or not. If not included, the selected header will jump to the top of the browser page and if included, the page will not scroll.

Lastly, you'll need to include some css to position the link image within the section header:
.ui-accordion {
    position: relative;
}
.ui-accordion .accordion-link {
    position: absolute;
    right: 2%;
    margin-top: 16px; /* adjust as needed to vertically center the icon */
    z-index: 1;
    width: 12px; /* approx 12x12 link icon */
    height: 12px;
    background: url(http://i57.tinypic.com/fyfns4.png) center center no-repeat;
}
Here is a demo of what it looks like: or, try this full screen version of the same demo

Tuesday, December 6, 2011

jQuery Pathslider

I just created this plugin that is a very basic UI Slider (similar to the jQuery slider), but it doesn't just allow you to move the handle horizontally and vertically, it will follow any shaped curve! If you've ever used Adobe Illustrator or CorelDraw then you probably recognize the Bezier curve below.

The handle, or grip as I call it, can be dragged along the black curve. For now, it only returns values from 0 (green dot) to 100 (red dot).


The plugin is in the early stages of development and still needs a lot of work, but it is usable now.

Check out the demo page, and the builder page. If you would like to help me out, submit an enhancement or issue, or fork a copy of the plugin on Github and send me a pull request! I'd love the input!

Monday, November 14, 2011

jQuery UI Side Scroller with buttons

Once again, I got up at 2:30am wide awake... that's what happens when I fall asleep at 9:30 LOL. Anyway, I was bored so I searched for something to do. I found a question on CSS Tricks asking how to add buttons to the jQuery UI Slider. So starting with the side scroll demo as a base, I put together this demo. That is all :P

Sunday, November 28, 2010

jQuery UI Keyboard Widget

If you need support, please contact me here:

I found this widget by Jeremy Satterfield which adds an virtual keyboard to any input or text area. I really liked it and had a lot of ideas that I wanted to add :P


So, I made a github repository, added a few of my ideas to the widget so now I present to you updated keyboard widget! (more updates to come!)


Here are some of the changes I made to version 1.5:
  • Changed class of preview window from 'ui-state-active' to 'ui-widget-content' because it looks bad in some themes.
  • Added 'ui-widget-content' class to the element (input or textarea).
  • Added International keyboard Layout (AltGr key) and expanded the keysets up to four.
  • Added more special/"action" keys:
    • Previous text only keys now have a companion symbol only key. The abbreviated names contain only a symbol so as to fit the layout style as desired.
    • Added alt key to allow accessing two additional key sets.
    • Changed name of {neg} to {sign}. This key changes the sign of the input.
    • Added tab key
  • Fixed positioning utility problem I added in the last version - show the popup before positioning (duh).
  • Added position option to allow positioning the keyboard anywhere around the input/textarea.
  • Added display option to support multiple languages or change key symbols.
  • Added actionClass option to allow changing the style of the accept and cancel keys.
  • Added lockInput option to lock or unlock the ability to access the preview window.
  • Added keyBinding option to change the keyboard key behaviour.
  • Added useCombos to enable the dead key emulation which allows entering diacritic key combinations.
  • Using the escape key now closes the keyboard.
  • Added mousewheel support to allow scrolling through the other keysets while hovering over a key.
  • Added ARIA support (may not be complete).
And on my to do list are:
  • Allow inserting text at the caret inside the preview window.
  • Add max length setting.
  • Add additional buttons to change key sets (similar to the alt key).
  • Add callbacks.
  • Add _destroy function.
  • Work on setting up one keyboard per layout to speed up initialization.

Wednesday, November 17, 2010

SqueezeBox

There is a very cool sliding accordion on Apple's Store site to compare various computers (click compare then scroll).

SqueezeBox is a jQuery plugin written to emulate that effect. It was designed to use the same themes available for jQuery UI, or you can add your own theme.

Click on the image or go to the demo page.

Features:
  • Easily change the theme but just loading in a different jQuery UI theme (Redmond theme shown).
  • SqueezeBox works with both collapsible and non-collapsible blocks.
  • Activate a header numerous ways:

    1. Use browser hash marks to target the header ID - used on initial page load.
    2. Click on any header to make it active - all headers above it will collapse using a smooth animation.
    3. Use the script to set the active header.
Download it from my github repository.

Wednesday, October 20, 2010

Web Search Widget

I started messing around with the jQuery UI widget factory and put together a widget that allows you to easily add a web search button to your site (no server side scripting required).


There isn't much for me to write here since I put almost all of the needed instructions on the WebSearch github page. I hope someone finds it useful!