页面树结构
转至元数据结尾
转至元数据起始

正在查看旧版本。 查看 当前版本.

与当前比较 查看页面历史

版本 1 下一个 »

示例说明
      在项目中有时需要对报表中的图片进行局部放大预览,该功能可以通过宏实现,如下图:

    

设置方法

  1. 在展现定制中,创建一张电子表格分析;
  2. 在浏览器的资源定制节点下,选中电子表格,右键选择 编辑宏 进入报表宏界面
  3. 在报表宏界面新建客户端模块,在弹出的新建模块对话框中选择对象为spreadSheetReport;事件为onRender;并把下面宏代码复制到代码区域。

宏类型

类型

对象

事件

ClientSide

spreadSheetReport

onRender

宏代码

var eventutil = jsloader.resolve("freequery.lang.eventutil");
function main(spreadsheetReport) {
    var source = spreadsheetReport.shapesMap["图片 1"].parentNode;
    var td = spreadsheetReport.getCell(3,6);
    var dest = document.createElement("div");
    dest.style.height="400px";
    dest.style.width="400px";
    dest.style.overflow ="hidden";
     
    td.appendChild(dest);
    //测试代码
    Amplifier.init(source,dest,5);
}
  
    var Amplifier = {
       //图片源
        source: null,
        //目的地,用来显示放大的效果
        dest: null,
        //放大的倍数,
        scale: 1,
        //初始化函数  source元素里面包含img对象, dest元素是空的
        init: function(source, dest,scale){
            this.source = source;
            this.dest = dest;
            this.scale = scale||2;
            //跟随鼠标移动的红色的框框
            var borderBox = document.createElement("div");
            borderBox.style.height = parseInt(this.source.style.height)/scale+"px";
            borderBox.style.width = parseInt(this.source.style.width)/scale+"px";
            borderBox.style.border = "solid red 1px";
            borderBox.style.position = "relative";
            this.source.appendChild(borderBox);                        //这个时候,放大框位于图片的下方
            //将框移动到图片上
            borderBox.style.top = -parseInt(this.source.style.height)+"px";
            borderBox.style.left = "0px" ;
             
            //放大后的图象
            var destImg = document.createElement('img');
            destImg.style.position = "relative";
            destImg.src = this.source.getElementsByTagName('img')[0].src;
            this.dest.appendChild(destImg);
            //图像的高度和宽度倍增,我发现先要执行this.dest.appendChild(destImg);
            //后再设置height,width才可以生效
            destImg.height = parseInt(this.source.style.height) * scale;
            destImg.width = parseInt(this.source.style.width) * scale;
        
            //对源图片添加onmousemove事件
            //this.source.onmousemove =       
            eventutil.addEventHandler(this.source,"mousemove",function (e){
               /**
               * 这里需要解释一下,ie下event.offsetX,offsetY表示鼠标相对当前事件触发元素左上角的偏移位置,
               * 通过改变borderBox.style.left和borderBox.style.top来改变红色的框框在源图中的位置,而且要
               * 把红色的框框限制在源图内。
               **/
          
            
              if (e.srcElement.nodeName != "IMG" )  //防止红框移动到鼠标位置,但是这样用起来有点卡
                return;
                 
               var offsetx = Amplifier.getEvent(e).offsetX;
               var offsety = Amplifier.getEvent(e).offsetY;
                
                if(offsetx>parseInt(borderBox.style.width)/2 && (parseInt(Amplifier.source.style.width)- offsetx)>parseInt(borderBox.style.width)/2){
                    borderBox.style.left = offsetx - parseInt(borderBox.style.width)/2+"px";
                }
                else if(offsetx<parseInt(borderBox.style.width)/2){
                    borderBox.style.left = "0px";
                }
                else{
                    borderBox.style.left = parseInt(Amplifier.source.style.width) - parseInt(borderBox.style.width)+"px";
                }
                 
                if(offsety>parseInt(borderBox.style.height)/2 && (parseInt(Amplifier.source.style.height)- offsety)>parseInt(borderBox.style.height)/2){
                    borderBox.style.top = -parseInt(Amplifier.source.style.height) + offsety - parseInt(borderBox.style.height)/2+"px";
                }
                else if(offsety<parseInt(borderBox.style.height)/2){
                    borderBox.style.top = -parseInt(Amplifier.source.style.height)+"px";
                }
                else{
                    borderBox.style.top = -parseInt(borderBox.style.height)+"px";
                }
                 
                //改变图片的style.left,style.top就可以精确控制被放大的部分。
                //就是移动放大图片的位置,使其显示应该现显示的部分
                destImg.style.left = - Math.abs(parseInt(borderBox.style.left) * scale+"px");
                destImg.style.top = -(parseInt(Amplifier.source.style.height) - Math.abs(parseInt(borderBox.style.top))) * scale+"px";
            });
         },
        //辅助函数,用来修复ie和ff不同的event模型
        getEvent: function(ev){
        return ev;
      }
         
    };

 

 

  • 无标签