简体   繁体   English

使用jQuery .html插入图像

[英]Inserting images using jQuery .html

I have a counter that counts in dollars. 我有一个计美元的柜台。 Each number, as well as the dollar sign and comma, are all displayed on the page using images. 每个数字以及美元符号和逗号都使用图像显示在页面上。 I use display: flex; 我使用display:flex; in my container div and flex: 1 1 auto; 在我的容器div和flex中:1 1 auto; on the divs that hold the images. 在保存图像的div上。 I use this CSS so that it all shrinks as more numbers are added. 我使用此CSS,以便随着添加更多数字而缩小。

Here is a jsfiddle to see the issue. 这是一个jsfiddle来查看问题。 https://jsfiddle.net/0mvy8hqo/ https://jsfiddle.net/0mvy8hqo/

Here is the CSS I am using 这是我正在使用的CSS

.i15-counter .i15-counter-inner { display: flex; }
.i15-counter .i15-counter-inner .i15-counter-number { flex: 1 1 auto; max-width: 110px; padding: 20px 0px; background-color: #333; }
.i15-counter .i15-counter-inner .i15-counter-dollar { flex: 1 1 auto; max-width: 76px; padding: 20px 0px; background-color: #333; }
.i15-counter .i15-counter-inner .i15-counter-comma { flex: 1 1 auto; max-width: 40px; padding: 20px 0px; background-color: #333; }
.i15-counter .i15-counter-inner .i15-counter-first {
  padding: 20px 0px 20px 20px;
  -webkit-border-top-left-radius: 5px;
  -webkit-border-bottom-left-radius: 5px;
  -moz-border-radius-topleft: 5px;
  -moz-border-radius-bottomleft: 5px;
  border-top-left-radius: 5px;
  border-bottom-left-radius: 5px;
}
.i15-counter .i15-counter-inner .i15-counter-last {
  padding: 20px 20px 20px 0px;
  -webkit-border-top-right-radius: 5px;
  -webkit-border-bottom-right-radius: 5px;
  -moz-border-radius-topright: 5px;
  -moz-border-radius-bottomright: 5px;
  border-top-right-radius: 5px;
  border-bottom-right-radius: 5px;
}

I also use jQuery to update the counter every second based on some math I have set up in my code. 我还使用jQuery根据我在代码中设置的一些数学运算来每秒更新一次计数器。 This all works just fine. 这一切都很好。 My only issue is that if the amount of images displayed on the page is enough that the images shrink then when I have my javascript replace the images with new images as the counter increases, the images start out at full size and then shrink to fit the container. 我唯一的问题是,如果页面上显示的图像数量足以使图像缩小,那么当我的JavaScript随计数器增加而用新图像替换图像时,图像将以全尺寸开始,然后缩小以适合容器。 So rather than it just looking like the number is changing, you have this expand/shrink effect that I would like to get rid of. 因此,您不仅具有看起来像数字在变化的效果,还具有我想摆脱的这种扩展/收缩效果。 This happens for about a minute then it stops. 这种情况发生大约一分钟,然后停止。 I am guessing the stopping has something to do with the browser's cache, but I am not sure. 我猜测停止与浏览器的缓存有关,但我不确定。

Here is the Javascript I am using. 这是我正在使用的Javascript。

function number_format (number, decimals, dec_point, thousands_sep) {
    // Strip all characters but numerical ones.
    number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}

function updateCounters(id) {
  var current = parseInt(jQuery('.i15-counter-'+id+'-current').val());
  var next = parseInt(jQuery('.i15-counter-'+id+'-next').val());
  var rate = parseInt(jQuery('.i15-counter-'+id+'-rate').val());
  var image_url = jQuery('.i15-counter-'+id+'-image-url').val();

  var new_current = current + rate;

  if(new_current <= next) {
    var new_current_formatted = number_format(new_current, 0);
    var new_current_formatted_adddollar = '$' + new_current_formatted;
    var new_current_split = new_current_formatted_adddollar.split("");
    //console.log(new_current_formatted);
    var numbercount = 1;
    var numbertotal = new_current_split.length;
    var counter_html = "";

    jQuery.each( new_current_split, function( i, val ) {
      var number_ends = "";
      if(numbercount == 1) {
        number_ends += " i15-counter-first";
      }

      if(numbercount == numbertotal) {
        number_ends += " i15-counter-last";
      }

      var anumber_class = "i15-counter-number";
      if(val == "$") { val = "dollar"; anumber_class = "i15-counter-dollar"; }
      if(val == ",") { val = "comma"; anumber_class = "i15-counter-comma"; }

      counter_html += '<div class="' + anumber_class + number_ends + '"><img src="' + image_url + 'counter_' + val + '.png" /></div>';
      numbercount++;
    });

    jQuery('.i15-counter-container-'+id).html(counter_html);
    jQuery('.i15-counter-'+id+'-current').val(new_current);
  }
}

jQuery(document).ready( function($) {

  window.setInterval(function(){
    jQuery(".i15-counter").each(function() {
      updateCounters(jQuery(this).data('id'));
    });
  }, 1000);

})

This is a loading problem. 这是一个加载问题。

Either use preloading or (better) specify a width: 使用预加载或(更好)指定宽度:

  var width = 110;
  if(val == "$") { val = "dollar"; anumber_class = "i15-counter-dollar"; width = 100; }
  if(val == ",") { val = "comma"; anumber_class = "i15-counter-comma"; width = 60; }
  counter_html += '<div class="' + anumber_class + number_ends + '">" +
      "<img src="' + image_url + 'counter_' + val + '.png" width=' + width + ' /></div>';

Note: i have not measured your actual widths, only roughly estimated them. 注意:我没有测量您的实际宽度,只是粗略估计了它们。

Like @ZPiDER mentioned you have to preload the assets 就像@ZPiDER提到的那样,您必须预先加载资产

I have attached the fiddle with preload functionality 我已经将小提琴附加了预加载功能

var images = new Array();
        function preload() {
            for (i = 0; i < preload.arguments.length; i++) {
                images[i] = new Image()
                images[i].src = preload.arguments[i]
            }
        }

https://jsfiddle.net/0mvy8hqo/1/ https://jsfiddle.net/0mvy8hqo/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM