Add a JavaScript display to the Home page to count down from 140 characters. (Rails Tutorial, 2nd Ed, Chapter 10, Exercise 7)

前端 未结 7 1094
别跟我提以往
别跟我提以往 2021-02-04 08:32

This exercise was a bit tricky. Figured I\'d post my solution to see if anyone did it differently or if there\'s anyone who knows a better way.

I\'m not sure on best pra

7条回答
  •  梦如初夏
    2021-02-04 09:13

    My microposts.js.coffee uses the jQuery .css method to change the color of the characters remaining based on the value of the variable remaining to more closely mirror the behavior of twitter

    updateCountdown = -> 
      remaining = 140 - jQuery("#micropost_content").val().length
      jQuery(".countdown").text remaining + " characters remaining"
      jQuery(".countdown").css "color", (if (140 >= remaining >= 21) then "gray")
      jQuery(".countdown").css "color", (if (21 > remaining >= 11) then "black")
      jQuery(".countdown").css "color", (if (11 > remaining)  then "red")
    
    jQuery ->
      updateCountdown()
      $("#micropost_content").change updateCountdown
      $("#micropost_content").keyup updateCountdown
    

    Thanks to all who answered before.

提交回复
热议问题