jQuery @变量?

发布于 2024-12-03 19:55:25 字数 3071 浏览 0 评论 0原文

有人在不同的站点上的代码出现了问题,我注意到他们的代码中大量使用了 @,我只是好奇它是什么以及他为什么使用它。

这是他的代码:

# check if supports css3
supports = (->
  div = document.createElement("div")
  vendors = "Khtml Ms O Moz Webkit".split(" ")
  len = vendors.length
  (prop) ->
    return true  if prop of div.style
    prop = prop.replace(/^[a-z]/, (val) ->
      val.toUpperCase()
    )
    while len--
      return true  if vendors[len] + prop of div.style
    false
)()

class HandleCarousel
  constructor: (wrapper, block_number) ->
    @block_number = block_number
    @wrapper = wrapper

  wrapper = null
  counter: 0
  block_number: 0
  loopTimeout = 5000
  loopTimer = null

  handleBlur: =>
    helpers.windowState = 'blur'
    window.clearTimeout(@loopTimer)
    @counter = 0
    # reset all
    target = @wrapper.find('ul')
    target.css({left: 0})
    target.clearQueue()
    target.find('ul').stop()
  handleFocus: =>
    if helpers.windowState == 'blur'
      helpers.windowState = 'focus'
      @start(5000)

  handleWindowState: ->
    $(window).blur(@handleBlur)
    $(window).focus(@handleFocus)

  loop: ->
    @counter++
    slider = @wrapper.find('ul')
    block = @wrapper.find('ul li')
    delta = parseInt(block.css('width'), 10) * @counter * (-1)
    if (@counter < @block_number)  
      # check css3 support
      if slider.hasClass 'css3'
        slider.css({'left': delta})
      else
        slider.animate({'left': delta},{duration: 500, easing: 'easeInSine'})
    else
      @counter = 0
      @wrapper.find('ul').css({left: 0})

    if (@counter is 0 || @counter is @block_number-1)
      if @counter is 0
        slider.removeClass 'css3'
      @loopTimeout = 2500
      #/
    else
      slider.addClass 'css3'
      @loopTimeout = 5000

    @start(@loopTimeout) #if helpers.windowState isnt 'blur'

  # start loop slider
  start: (time) ->
    console.log 'start'
    @loopTimer = window.setTimeout( =>
      @loop()
    , time)



helpers = {
  windowState: 'focus'
  iDevice: /iphone|ipad/i.test(navigator.userAgent.toLowerCase())
  updateOrientation: ->
    location.reload()
  hasTouch : /android|iphone|ipad/i.test(navigator.userAgent.toLowerCase())
  supportsTouch: -> # "'createTouch' in document" will return true in Apple's Mobile Safari. Otherwise detect Android directly.
    android = navigator.userAgent.indexOf('Android') != -1
    return android || !!('createTouch' in document)

}# /helpers


jQuery ->
  window.console ?=
    log:-> #patch so console.log() never causes error even in IE.

  if $('.carousel').length>0
    target = $('.carousel')
    if supports('textShadow')
      target.find('ul').addClass 'css3'
    firstImg = target.find('ul li:first').html()
    console.log firstImg
    target.find('ul').append('<li>'+firstImg+'</li>')
    carousel_items = target.find('li').length
    if carousel_items > 2
      carousel = new HandleCarousel(target, carousel_items)
      carousel.handleWindowState()
      carousel.start(carousel.loopTimeout)

Someone had a problem with their code on a different site and I noticed the use of @ a lot in their code and I was merely curious to what it is and why he is using it.

Here is his code:

# check if supports css3
supports = (->
  div = document.createElement("div")
  vendors = "Khtml Ms O Moz Webkit".split(" ")
  len = vendors.length
  (prop) ->
    return true  if prop of div.style
    prop = prop.replace(/^[a-z]/, (val) ->
      val.toUpperCase()
    )
    while len--
      return true  if vendors[len] + prop of div.style
    false
)()

class HandleCarousel
  constructor: (wrapper, block_number) ->
    @block_number = block_number
    @wrapper = wrapper

  wrapper = null
  counter: 0
  block_number: 0
  loopTimeout = 5000
  loopTimer = null

  handleBlur: =>
    helpers.windowState = 'blur'
    window.clearTimeout(@loopTimer)
    @counter = 0
    # reset all
    target = @wrapper.find('ul')
    target.css({left: 0})
    target.clearQueue()
    target.find('ul').stop()
  handleFocus: =>
    if helpers.windowState == 'blur'
      helpers.windowState = 'focus'
      @start(5000)

  handleWindowState: ->
    $(window).blur(@handleBlur)
    $(window).focus(@handleFocus)

  loop: ->
    @counter++
    slider = @wrapper.find('ul')
    block = @wrapper.find('ul li')
    delta = parseInt(block.css('width'), 10) * @counter * (-1)
    if (@counter < @block_number)  
      # check css3 support
      if slider.hasClass 'css3'
        slider.css({'left': delta})
      else
        slider.animate({'left': delta},{duration: 500, easing: 'easeInSine'})
    else
      @counter = 0
      @wrapper.find('ul').css({left: 0})

    if (@counter is 0 || @counter is @block_number-1)
      if @counter is 0
        slider.removeClass 'css3'
      @loopTimeout = 2500
      #/
    else
      slider.addClass 'css3'
      @loopTimeout = 5000

    @start(@loopTimeout) #if helpers.windowState isnt 'blur'

  # start loop slider
  start: (time) ->
    console.log 'start'
    @loopTimer = window.setTimeout( =>
      @loop()
    , time)



helpers = {
  windowState: 'focus'
  iDevice: /iphone|ipad/i.test(navigator.userAgent.toLowerCase())
  updateOrientation: ->
    location.reload()
  hasTouch : /android|iphone|ipad/i.test(navigator.userAgent.toLowerCase())
  supportsTouch: -> # "'createTouch' in document" will return true in Apple's Mobile Safari. Otherwise detect Android directly.
    android = navigator.userAgent.indexOf('Android') != -1
    return android || !!('createTouch' in document)

}# /helpers


jQuery ->
  window.console ?=
    log:-> #patch so console.log() never causes error even in IE.

  if $('.carousel').length>0
    target = $('.carousel')
    if supports('textShadow')
      target.find('ul').addClass 'css3'
    firstImg = target.find('ul li:first').html()
    console.log firstImg
    target.find('ul').append('<li>'+firstImg+'</li>')
    carousel_items = target.find('li').length
    if carousel_items > 2
      carousel = new HandleCarousel(target, carousel_items)
      carousel.handleWindowState()
      carousel.start(carousel.loopTimeout)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深府石板幽径 2024-12-10 19:55:25

那不是 jQuery 或 JavaScript。它是 CoffeeScript,最终会转换为 JavaScript。

@ 用于引用当前上下文的属性 (this)。所以 CoffeeScript 的,

@something

相当于 JavaScript 的

this.something

That is not jQuery or JavaScript. It is CoffeeScript and will eventually get converted to JavaScript.

@ is used to refer to properties on the current context (this). So CoffeeScript's,

@something

is equivalent to JavaScript's

this.something
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文