GithubHelp home page GithubHelp logo

jackmcdowell / jquery.fancyspinbox Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mayakokits/jquery.fancyspinbox

0.0 2.0 0.0 38 KB

A small jQuery plugin for spinbox-style select dropdowns

License: MIT License

CSS 39.11% JavaScript 60.89%

jquery.fancyspinbox's Introduction

jquery.fancyspinbox

This lightweight jQuery plugin turns a select dropdown into a spinbox with up and down arrows (and an optional dropdown menu), a simple flat design and items sliding into view as you select them. It uses CSS transitions with a jQuery fallback and is accessible to keyboards and screen readers.

You can see a demo here.

This plugin is under development. A To Do list might include:

  • a remove() function
  • support for disabled options
  • a dropdown scrollbar in case of many options or limited height

Install

As you might have guessed, this depends on jQuery. Apart from that, all you need is jquery.fancyspinbox.js and the styles in jquery.fancyspinbox.css. If you want to style the Spinbox, I recommend tweaking the Sass file.

Install with Bower

$ bower install jquery.fancyspinbox

Usage

The markup you need looks familiar:

<select id="my-menu">
	<option value="apples">Apples</option>
	<option value="bananas" selected>Bananas</option>
	<option value="chocolate">Chocolate</option>
</select>

On clients without Javascript enabled, it will gracelessly degrade to an unstyled generic select field.

You initialize the plugin like this:

var myMenu = $('#my-menu').fancyspinbox();

This returns the original select element which will stay in the DOM and which you can refer to as usual:

myMenu.on('change', function(){
		console.log('You choose option nr. ' + this.selectedIndex + ' which has the value "' + this.value + '"');
	});

If you want to programatically change the selected Index, you should use the methods on the plugin object:

myMenu.data('fancyspinbox').select(1);   // Select the second item in the list

You can also give it a spin:

// Those return the new selected index and `false` if it's the end of the list.
myMenu.data('fancyspinbox').up();
myMenu.data('fancyspinbox').down();

The add() method lets you add items dynamically. The value argument is optional.

myMenu.data('fancyspinbox').add('text', 'value');

If you need to access the spinbox element, use the element object.

myMenu.data('fancyspinbox').element.css({'border-color': 'red'});

HTML in items

Unlike standard <option> options, fancyspinbox options can contain HTML. Just set a data-text attribute.

<select id="my-menu">
	<option value="apples" data-text="some <strong>really tasty</strong> apples">some really tasty apples</option>
	<option value="bananas">Bananas</option>
	<option value="chocolate">Chocolate</option>
</select>

Options

You can pass options as you initialize the plugin:

$('#my-menu').fancyspinbox({
	cycle: true,
	items: [
			{value: 'grapes', text: 'Sweet grapes'},
			{value: 'potatoes', text: '<strong>Hot</strong> potatoes'}
		]
	});

Here is a list of the options available:

Property Default value Description
items [] An array of items to add to the list on initialisation. Items can be strings or objects like {'value': 'foo', 'text': 'bar'}
spinButtons true Display spin buttons.
dropdownButton false Display a dropdown button. Overrides the spinButtons option and sets dropdown to true.
dropdown true Display the dropdown.
cycle false Cycle through the items, don't stop at the end of the list.
scroll true Animate the spinbox.
scrollDuration 300 Duration of the scroll transition.
flipSpinDirection false Flip the spin button's direction.
focusOnSpin false Give the focus to the spinbox if a spin button is clicked.

jquery.fancyspinbox's People

Watchers

James Cloos avatar Jack McDowell avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.