1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- $(document).ready(function () {
- 'use strict';
- // Enable hover effect on the style switcher
- $('#switcher').hover(function() {
- $(this).addClass('hover');
- }, function() {
- $(this).removeClass('hover');
- });
- // Allow the style switcher to expand and collapse.
- var toggleSwitcher = function(event) {
- if (!$(event.target).is('button')) {
- $('#switcher button').toggleClass('hidden');
- }
- };
- $('#switcher').on('click', toggleSwitcher);
- // Simulate a click so we start in a collapsed state.
- $('#switcher').click();
- // The setBodyClass() function changes the page style.
- // The style switcher state is also updated.
- var setBodyClass = function (className) {
- $('body').removeClass().addClass(className);
- $('#switcher button').removeClass('selected');
- $('#switcher-' + className).addClass('selected');
- $('#switcher').off('click', toggleSwitcher);
- if (className === 'default') {
- $('#switcher').on('click', toggleSwitcher);
- }
- };
- // begin with the switcher-default button "selected"
- $('#switcher-default').addClass('selected');
- // Map key codes to their corresponding buttons to click
- var triggers = {
- D: 'default',
- N: 'narrow',
- L: 'large'
- };
- // Call setBodyClass() when a button is clicked.
- $('#switcher').click(function(event) {
- if ($(event.target).is('button')) {
- var bodyClass = event.target.id.split('-')[1];
- setBodyClass(bodyClass);
- }
- });
- // Call setBodyClass() when a key is pressed.
- // Use keydown/keyup to identify key, keypress to identify resulting letter.
- $(document).keyup(function (event) {
- var key = String.fromCharCode(event.which);
- if (key in triggers) {
- setBodyClass(triggers[key]);
- }
- });
- // Exercise 1.
- $('.author').click(function () {
- $(this).toggleClass('selected');
- });
- // Exercise 2.
- $('.chapter-title').dblclick(function () {
- $(this).siblings().toggleClass('hidden');
- });
- });
|