lundi 24 avril 2017

My 2 coffee files interfering with each other in Rails

I know this is a little awkward question but the problem here is that in my rails app, I write 2 coffee script files both require window.onscroll fn.

when I implement them one at a tym both works fine, but when I include both, the script stops working... I know its a novice q. but plz help

Thank you.

here is the first script

rightBarControl = ->
  windowHeight = $(window).height()
  scrollHeight = $(window).scrollTop()
  rightBarWidth = $('#index_top_confession_div').width()
  #20% of .main width
  rightBarHeight = $('#index_top_confession_div').outerHeight()
  rightBarOffset = $('#index_confessions').offset().left + $('#index_confessions').outerWidth()
  rightBarTop = 75
  #30 because .head is 30px high
  if windowHeight - 75 < rightBarHeight
    #Again including 30 because of .head
    rightBarTop = windowHeight - rightBarHeight
  if windowHeight + scrollHeight - 75 >= rightBarHeight
    $('#index_top_confession_div').css
      position: 'fixed'
      left: rightBarOffset
      top: rightBarTop
  else
    $('#index_top_confession_div').css
      position: 'static'
      left: rightBarOffset
      top: rightBarTop
  return

$('#search').addClass 'form-control'
$(window).scroll rightBarControl
#Run control on window scroll
$(window).resize rightBarControl

The second script file..

 $(document).ready ->
  if $('.pagination').length
    $(window).scroll ->
      console.log('hey');
      url = undefined
      url = $('.pagination .next_page').attr('href')
      if url and $(window).scrollTop() > $(document).height() - $(window).height() - 200
        $('.pagination').html '<img src = \'/uploads/loader/loader.gif\' alt=\'loading...\'/>'
        return $.getScript(url)
      return
    return $(window).scroll()
  return

Aucun commentaire:

Enregistrer un commentaire