.toggle()
Syntax Variants
Parameters
- cssClasses
- a string containing space-separated CSS class names to toggle. Classes are disabled in the first state and enabled in the second.
- state1
- a property map in
set()syntax describing the initial state of the properties. The properties will automatically be set when the toggle() function is created. The properties will be set for all elements of the list. - state2
- a property map describing the second state of the properties. Uses
set()syntax, like the other state. - durationMs (optional)
- if set, the duration of the animation in milliseconds. By default, there is no animation and the properties will be changed immediately.
- linearity (optional)
- defines whether the animation should be linear (1), very smooth (0) or something in between. Default: 0. Ignored if durationMs is 0.
- interpolationFunc (optional)
- an interpolation
function(startValue, endValue, t)for the animation which will be called every time an interpolated value is required:- startValue
- The start value of the transition.
- endValue
- The end value of the transition.
- t
- A value between 0 and 1 that specifies the state of the transition.
- (callback return value)
- The value at the time t.
- (return value)
- a toggle function
function(newState)that will toggle between the two states, or set a specific state.- newState (optional)
- If a boolean true or false is given, the toggle will set the first or second state respectively. If called with any other value, or without a value, the function toggles to the other state.
Description
Creates a function that switches between the two given states for the list. The states use the set() property syntax. You can also
just pass a string of CSS classes, as you do with set().
If no duration is given, the returned function changes the state immediately using set(). If a duration has been passed, the returned function
uses animate() to smoothly transition the state. If the returned function is invoked while an animation is running, it interrupts the
animation and returns to the other state.
Example
Creates a toggle function that changes the background color of the page.
var light = $('body').set({$backgroundColor: #000}, {$backgroundColor: #fff});
light(); // toggles state to second state
light(false); // sets first state (background color to #000).
light(true); // sets second state (background color to #fff).
light(); // toggles state to first state
Example
Takes the previous function, but adds it as an onclick event handler that toggles the color.
var light = $('body').toggle({$backgroundColor: #000}, {$backgroundColor: #fff});
$('#mySwitch').on('click', light);
Example
Using an animated transition by passing a duration:
var dimmer = $('body').toggle({$backgroundColor: #000}, {$backgroundColor: #fff}, 500);
$('#mySwitch').on('click', dimmer);
Example
Toggling CSS classes using the full syntax:
var t = $('#myElement').toggle({$: '-myClass1 -myClass2'}, {$: '+myClass1 +myClass2'});
$('#myController').on('click', t);
Example
There is a shortcut for toggling CSS classes. Just list them space-separated in a string:
var t = $('#myElement').toggle('myClass1 myClass2');
See also..
dial()is a similar function that allows you to smoothly interpolate between two states.
Comments
comments powered by DisqusFunctions
- $() Web
- list.length Web, Util
- $$() Web
- $.getCookie()
- $.loop() Web
- $.off() Web
- $.parseJSON() Web
- $.ready() Web
- $.request() Web
- $.setCookie()
- $.toJSON() Web
- $.wait()
- .add() Web
- .addAfter() Web
- .addBefore() Web
- .addFront() Web
- .animate() Web
- .array() Util
- .call() Util
- .clone() Web
- .collect() Web, Util
- .contains() Util
- .dial() Web
- .each() Web, Util
- .endsWith() Util
- .equals() Util
- .fill() Web
- .filter() Web, Util
- .find() Web, Util
- .findLast() Web, Util
- .get() Web
- .hide() Web
- .ht()
- .intersection() Util
- .is() Web
- .join() Util
- .map() Util
- .merge() Util
- .next() Web
- .not() Web
- .offset()
- .on() Web
- .onChange() Web
- .onClick() Web
- .onFocus() Web
- .onOver() Web
- .only()
- .per() Util
- .reduce() Util
- .remove() Web
- .replace() Web
- .reverse() Util
- .select() Web
- .set() Web
- .show() Web
- .sort() Util
- .startsWith() Util
- .sub() Web, Util
- .text() Web
- .toObject() Util
- .toggle() Web
- .trav() Web
- .trigger() Web
- .uniq() Util
- .unite() Util
- .up() Web
- .values() Web
- EE() Web
- HTML() Web
- M Web, Util
- MINI.getter Web
- MINI.setter Web
- Minified Lists Web, Util
- Promise Web, Util
- _() Util
- _.bind() Util
- _.copyObj() Util
- _.dateAdd() Util
- _.dateClone() Util
- _.dateDiff() Util
- _.dateMidnight() Util
- _.eachObj() Util
- _.escapeHtml() Util
- _.escapeRegExp() Util
- _.extend() Util
- _.filterObj() Util
- _.format() Util
- _.formatHtml() Util
- _.formatValue() Util
- _.isBool() Util
- _.isDate() Util
- _.isEmpty() Util
- _.isFunction() Util
- _.isList() Util
- _.isNumber() Util
- _.isObject() Util
- _.isString() Util
- _.isValue() Util
- _.keys() Util
- _.mapObj() Util
- _.pad() Util
- _.parseDate() Util
- _.parseNumber() Util
- _.partial() Util
- _.promise()
- _.range()
- _.template() Util
- _.toString() Util
- _.trim() Util
- _.values() Util
- define() Web, Util
- promise.always()
- promise.error() Web, Util
- promise.fire()
- promise.stop()
- promise.then() Web
- require() Web, Util
- How to...