【问题标题】:currency ticker without white space没有空格的货币代码
【发布时间】:2017-06-11 02:17:45
【问题描述】:

我想创建一个货币代码。查询从 yahoo Finance API 检索值并将它们显示为文本。主要问题是文本末尾的空白。选框脚本marquee plugin 解决了差距问题。 setInterval 会打乱移动文本,因为它从头开始。

     $(document).ready(function() {
       //StockPriceTicker();
       setInterval(StockPriceTicker, 5000);
     });

     function StockPriceTicker() {
       var Symbol = "",
         CompName = "",
         Price = "",
         ChnageInPrice = "",
         PercentChnageInPrice = "";
       var CNames = "^FTSE,HSBA.L,SL.L,BATS.L,BLND.L,FLG.L,RBS.L,RMG.L,VOD.L";
       var flickerAPI = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22" + CNames + "%22)&env=store://datatables.org/alltableswithkeys";
       var StockTickerHTML = "";

       var StockTickerXML = $.get(flickerAPI, function(xml) {
         $(xml).find("quote").each(function() {
           Symbol = $(this).attr("symbol");
           $(this).find("Name").each(function() {
             CompName = $(this).text();
           });
           $(this).find("LastTradePriceOnly").each(function() {
             Price = $(this).text();
           });
           $(this).find("Change").each(function() {
             ChnageInPrice = $(this).text();
           });
           $(this).find("PercentChange").each(function() {
             PercentChnageInPrice = $(this).text();
           });

           StockTickerHTML += "<span>" + CompName + " " + Price + "</span>";
         });

         $(".marquee div").html(StockTickerHTML);
         //$("#dvStockTicker").jStockTicker({interval: 30, speed: 2});
       });
     }
body {
  margin: 20px;
}
.marquee {
  height: 25px;
  width: 420px;
  overflow: hidden;
  position: relative;
}
.marquee div {
  display: block;
  width: 200%;
  height: 30px;
  position: absolute;
  overflow: hidden;
  animation: marquee 5s linear infinite;
}
.marquee span {
  float: left;
  width: 50%;
}
@keyframes marquee {
  0% {
    left: 0;
  }
  100% {
    left: -100%;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="marquee">
  <div>

  </div>
</div>

提前致谢。

【问题讨论】:

  • 没有实际运行的东西很难,尽管我立即反应的一件事是.marquee span { float: left; width: 50%; }。删除width: 50% 或将其更改为.marquee span { display: inline-block; } 可能会解决您的问题,因为这样做会使span 适应其内容
  • 我知道我尝试了很多都没有运气。我希望有人能提供帮助。
  • 您能否让我知道给出的答案有什么问题,以便我能够进行调整并让您接受?

标签: jquery html css currency ticker


【解决方案1】:

问题是你给span一个固定的宽度,如果文本更小,每个末尾会有一个很大的空白空间,所以通过调整.marquee span这样的规则来解决这个问题

.marquee span {
    display: inline-block;
}
.marquee span ~ span::before {
    content:'|';
    color: red;
    padding: 0 5px
}

文字中断是.marquee div的固定宽度造成的,所以我也做了一些调整

.marquee div {
    display: inline-block;
    padding-left: 100%;          /*  start from right, can be removed  */
    animation: marquee 25s linear 2s infinite;
}

示例 sn-p

$(document).ready(function () {
            //StockPriceTicker();
            setInterval(StockPriceTicker , 1000);
        });
		
        function StockPriceTicker() {
            var Symbol = "", CompName = "", Price = "", ChnageInPrice = "", PercentChnageInPrice = ""; 
            var CNames = "^FTSE,HSBA.L,SL.L,BATS.L,BLND.L,FLG.L,RBS.L,RMG.L,VOD.L";
            var flickerAPI = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22" + CNames + "%22)&env=store://datatables.org/alltableswithkeys";
            var StockTickerHTML = "";
            
            var StockTickerXML = $.get(flickerAPI, function(xml) {
                $(xml).find("quote").each(function () {
                    Symbol = $(this).attr("symbol");
                    $(this).find("Name").each(function () {
                        CompName = $(this).text();
                    });
                    $(this).find("LastTradePriceOnly").each(function () {
                        Price = $(this).text();
                    });
                    $(this).find("Change").each(function () {
                        ChnageInPrice = $(this).text();
                    });
                    $(this).find("PercentChange").each(function () {
                        PercentChnageInPrice = $(this).text();
                    });
                    
                    StockTickerHTML += "<span>"+CompName+" "+Price+"</span>";
                });
			
                $(".marquee div").html(StockTickerHTML);
                //$("#dvStockTicker").jStockTicker({interval: 30, speed: 2});
            });
        }
body { margin: 20px; }

.marquee {
    margin: 0 auto;
    white-space: nowrap;
    overflow: hidden;
    box-sizing: border-box;
    border: 1px green solid;
}

.marquee span {
    display: inline-block;
}
.marquee span ~ span::before {
    content:'*';
    padding: 0 25px;
}

.marquee div {
    display: inline-block;
    padding-left: 100%;
    animation: marquee 12s linear 1s infinite;
}

@keyframes marquee {
    0%   { transform: translate(0, 0); }
    100% { transform: translate( -100%, 0); }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="marquee">
  <div>
  </div>
</div>

根据评论更新

$(document).ready(function () {
            //StockPriceTicker();
            setInterval(StockPriceTicker , 5000);
        });
		
        function StockPriceTicker() {
            var Symbol = "", CompName = "", Price = "", ChnageInPrice = "", PercentChnageInPrice = ""; 
            var CNames = "^FTSE,HSBA.L,SL.L,BATS.L,BLND.L,FLG.L,RBS.L,RMG.L,VOD.L";
            var flickerAPI = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22" + CNames + "%22)&env=store://datatables.org/alltableswithkeys";
            var StockTickerHTML = "";
            
            var StockTickerXML = $.get(flickerAPI, function(xml) {
                $(xml).find("quote").each(function () {
                    Symbol = $(this).attr("symbol");
                    $(this).find("Name").each(function () {
                        CompName = $(this).text();
                    });
                    $(this).find("LastTradePriceOnly").each(function () {
                        Price = $(this).text();
                    });
                    $(this).find("Change").each(function () {
                        ChnageInPrice = $(this).text();
                    });
                    $(this).find("PercentChange").each(function () {
                        PercentChnageInPrice = $(this).text();
                    });
                    
                    StockTickerHTML += "<span>"+CompName+" "+Price+"</span>";
                });
			
                $(".marquee div").html(StockTickerHTML);
                //$("#dvStockTicker").jStockTicker({interval: 30, speed: 2});
            });
        }
body { margin: 20px 0; }

.marquee {
    margin: 0 auto;
    white-space: nowrap;
    overflow: hidden;
    box-sizing: border-box;
    border: 1px green solid;
}
.marquee span {
    display: inline-block;
    background: lightgray;
}
.marquee span ~ span::before {
    content:'|';
    color: red;
    padding: 0 5px;
}
.marquee div {
    display: inline-block;
    animation: marquee 6s linear 2s infinite;
}

@keyframes marquee {
    0%   { transform: translate(0, 0); }
    100% { transform: translate( calc(-100% + 100vw), 0); }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="marquee">
  <div>
  </div>
</div>

【讨论】:

  • 好的,你可以注意到有东西打断了文本的平滑滚动。
  • @MariosNikolaou 更新了我的答案
  • 感谢您的帮助,如果您注意到文本末尾有空格,我该如何删除它?
  • @MariosNikolaou 添加了 2:nd 示例,该示例是否按预期工作?
  • 选框宽度应该是100%,我测试过,太慢了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-29
  • 1970-01-01
  • 2011-06-27
  • 2014-01-17
  • 1970-01-01
  • 2012-09-04
  • 2014-12-07
相关资源
最近更新 更多