Javascript does not work from a bootstrap template in rails

前端 未结 3 955
心在旅途
心在旅途 2021-01-16 04:47

I have created a new rails app and installed all the necessary gems. Added all the files. Updated the application.js file. Everything works but for some reason all the javas

3条回答
  •  Happy的楠姐
    2021-01-16 05:29

    The tricky part is around the welcomes.js and the turbolinks. Convert your welcomes.js to coffee (with js2coffee for example) and then copy it in a file like home.coffee on your app/assets/javascripts. The key is at the first line to add $(document).on "turbolinks:load", ->

    $(document).on "turbolinks:load", ->
      (($) ->
        'use strict'
        # Start of use strict
        # jQuery for page scrolling feature - requires jQuery Easing plugin
        $('a.page-scroll').bind 'click', (event) ->
          $anchor = $(this)
          $('html, body').stop().animate { scrollTop: $($anchor.attr('href')).offset().top - 50 }, 1250, 'easeInOutExpo'
          event.preventDefault()
          return
        # Highlight the top nav as scrolling occurs
        $('body').scrollspy
          target: '.navbar-fixed-top'
          offset: 51
        # Closes the Responsive Menu on Menu Item Click
        $('.navbar-collapse ul li a').click ->
          $('.navbar-toggle:visible').click()
          return
        # Offset for Main Navigation
        $('#mainNav').affix offset: top: 100
        # Initialize and Configure Scroll Reveal Animation
        window.sr = ScrollReveal()
        sr.reveal '.sr-icons', {
          duration: 600
          scale: 0.3
          distance: '0px'
        }, 200
        sr.reveal '.sr-button',
          duration: 1000
          delay: 200
        sr.reveal '.sr-contact', {
          duration: 600
          scale: 0.3
          distance: '0px'
        }, 300
        # Initialize and Configure Magnific Popup Lightbox Plugin
        $('.popup-gallery').magnificPopup
          delegate: 'a'
          type: 'image'
          tLoading: 'Loading image #%curr%...'
          mainClass: 'mfp-img-mobile'
          gallery:
            enabled: true
            navigateByImgClick: true
            preload: [
              0
              1
            ]
          image: tError: 'The image #%curr% could not be loaded.'
        return
      ) jQuery
    

    and everything works fine! Don't change anything else. Setup your js manifest with the correct js files for your template and you're ready.

提交回复
热议问题