main.js 1.9 KB

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