main.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. var $ = window.jQuery
  2. var $window = $(window)
  3. var sectionTop = $('.top').outerHeight() + 20
  4. var $createDestroy = $('#switch-create-destroy')
  5. function capitalize (string) {
  6. return string.charAt(0).toUpperCase() + string.slice(1)
  7. }
  8. window.hljs.initHighlightingOnLoad()
  9. $(function () {
  10. $('a[href*=\'#\']').on('click', function (event) {
  11. event.preventDefault()
  12. var $target = $($(this).attr('href').slice('#'))
  13. if ($target.length) {
  14. $window.scrollTop($target.offset().top - sectionTop)
  15. }
  16. })
  17. $('input[type="checkbox"], input[type="radio"]')
  18. .not('[data-switch-no-init]')
  19. .bootstrapSwitch()
  20. $('[data-switch-get]').on('click', function () {
  21. var type = $(this).data('switch-get')
  22. window.alert($('#switch-' + type).bootstrapSwitch(type))
  23. })
  24. $('[data-switch-set]').on('click', function () {
  25. var type
  26. type = $(this).data('switch-set')
  27. $('#switch-' + type).bootstrapSwitch(type, $(this).data('switch-value'))
  28. })
  29. $('[data-switch-toggle]').on('click', function () {
  30. var type = $(this).data('switch-toggle')
  31. $('#switch-' + type).bootstrapSwitch('toggle' + capitalize(type))
  32. })
  33. $('[data-switch-set-value]').on('input', function (event) {
  34. var type, value
  35. event.preventDefault()
  36. type = $(this).data('switch-set-value')
  37. value = $.trim($(this).val())
  38. if ($(this).data('value') === value) {
  39. return
  40. }
  41. $('#switch-' + type).bootstrapSwitch(type, value)
  42. })
  43. $('[data-switch-create-destroy]').on('click', function () {
  44. var isSwitch
  45. isSwitch = $createDestroy.data('bootstrap-switch')
  46. $createDestroy.bootstrapSwitch((isSwitch ? 'destroy' : null))
  47. $(this).button((isSwitch ? 'reset' : 'destroy'))
  48. })
  49. $('#confirm').bootstrapSwitch({
  50. size: 'large',
  51. onSwitchChange: function (event, state) {
  52. event.preventDefault()
  53. return console.log(state, event.isDefaultPrevented())
  54. }
  55. })
  56. })