【问题标题】:HTML / CSS - Dynamically abbreviate text based on screen width [duplicate]HTML / CSS - 根据屏幕宽度动态缩写文本[重复]
【发布时间】:2020-01-23 08:07:26
【问题描述】:

我正在尝试根据屏幕分辨率缩写简短的时间戳短语。

例如,这里有一些短语及其各自的缩写。

“1 小时前”=> 1 小时

"2 天前" => 2d

"40 分钟前" => 40m

如何在屏幕宽度缩小时动态缩写文本,同时仍然能够根据时间关键字(小时/小时、天/天、分钟)确定将哪个字母连接到数字的末尾/分钟)在短语中?

【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    你可以这样做:

     let text = "1 hour ago"
     let text2 = "2 days ago"
     let textSplited = text.split(' ')// RESULT -> ARRAY(3) ["1", "hour", "ago"]
     let textSplited2 = text2.split(' ')// RESULT -> ARRAY(3) ["2", "days", "ago"]
     const finalResult = `${textSplited[0]}${textSplited[1][0]}`
     const finalResult2 = `${textSplited2[0]}${textSplited2[1][0]}`
     console.log(finalResult) // RESULT => 1h
     console.log(finalResult2) // RESULT => 2d
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-26
      • 2012-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      相关资源
      最近更新 更多