duanmao9918 2012-11-19 19:54
浏览 79

JQuery获取屏幕大小和PHP生成图像¿很好的方式?

Im developing a stadistics panel for users, where the images shown are bandwith usage stadistics (from one day, from 2 days, etc.). I was going to use only php, with frames, but I would like to have a more dinamic design so the images and the design could fit the user screen size, so I'm trying with JQuery to ask for the user resolution and then generate the exact images with php.

The Javascript does the screen size reading (imgsize()) and passes the values to the php script (stats_generatemain(tipo)). The php script generates the image and then the Javascript reloads the image div container (#mstats) to show the new php generated image.

Is this a good way to do it? Would you do it another way?

Thanks very much.

<script>
  $(document).ready(function () {
    $.ajaxSetup({
      cache: false
    });

    function imgsize() {

      var myWidth = screen.availWidth;
      var myHeight = screen.availHeight;
      myRatio = myWidth / myHeight;
      myWidth = myWidth - 504;
      myHeight = myWidth / myRatio;

      var dim = new Array(2);
      dim[0] = myWidth;
      dim[1] = myHeight;
      return dim;
    }

    function mstats_resize_start() {
      var dim = imgsize();
      $('#mstats').width(dim[0]).height(430);
    }

    function stats_generatemain(tipo) { 
        if(tipo==''){tipo=1;}
        else{tipo=parseInt(tipo);}
        var dim = imgsize();
        $.ajax({ 
             url: 'stats_generatemain.php',
             type: 'get',
             data: {imsi: '214290000000003',tipo: tipo, w: dim[0], h: 430},
             beforeSend: function(){
                $("#mstats").hide();
                $('#mloading').show();
             },
             success: function(){
                var href = "./TEMP/trafico-214290000000003-main.jpg?" + Math.random();
                $("#mstats").attr("src", href);
             },
             complete: function(){
                $('#mloading').hide();
                $("#mstats").show();
             }
        });
    }

    stats_generatemain(1);

    $("a.link").live('click', function (e) {
      e.preventDefault();
      var tipo = this.href.split(/=/)[1];
      stats_generatemain(tipo);
    });

    $('li > a').click(function () {
      $('li').removeClass();
      $(this).parent().addClass('active');
    });


    jQuery.event.add(window, "load", mstats_resize_start);

  });
</script>
  • 写回答

2条回答 默认 最新

  • doukun1450 2013-03-09 07:05
    关注

    I think you may be over-thinking this... why not just use responsive design / fluid images? Apply a class to your image that uses the following css:

    max-width:100%;
    

    Height will be automatically calculated.

    I'd suggest looking at Twitter Bootstrap if you want a nice responsive design framework. If you're not too good at designing things, bootstrap can help take much of the work load off and allow you to focus on the programming side.

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口