等比缩放

缩放!所有的东西都可以缩放!

一些比较炫的响应式网站会在一定范围内有缩放效果。当然,js可以搞定~

以前就用过的全屏缩放类:

// CLASS
function imgzoom(srcWidth,srcHeight,maxWidth,maxHeight){
     this.srcWidth=srcWidth;//获得原始宽度
     this.srcHeight=srcHeight;//获得原始高度
     this.maxWidth=maxWidth;//获得限定宽度
     this.maxHeight=maxHeight;//获得限定高度
     this.newWidth;
     this.newHeight;
}

imgzoom.prototype.getHeightSize=function(){
    this.newHeight=this.maxHeight;
    this.newWidth=(this.srcWidth*this.maxHeight)/this.srcHeight;
    return [this.newWidth,this.newHeight];
}
imgzoom.prototype.getSize=function (){

    this.newWidth=this.maxWidth;
    this.newHeight=(this.srcHeight*this.maxWidth)/this.srcWidth;

    if(this.newHeight<this.maxHeight){
        this.newHeight=this.maxHeight;
        this.newWidth=(this.srcWidth*this.maxHeight)/this.srcHeight;
    }
    var srcNum=this.srcWidth/this.srcHeight;
    var maxNum=this.maxWidth/this.maxHeight;

    if(srcNum>=maxNum){
        //比较高宽比例,确定以宽或者是高为基准进行计算。
        if(this.srcWidth>this.maxWidth){//以宽为基准开始计算,
            //当宽度大于限定宽度,开始缩放
            this.newWidth=this.maxWidth;
            this.newHeight=(this.srcHeight*this.maxWidth)/this.srcWidth
        }else{
            //当宽度小于限定宽度,直接返回原始数值。
            this.newWidth=this.srcWidth;
            this.newHeight=this.srcHeight;
        }

    }else{
        if(this.srcHeight>this.maxHeight){//以高为基准,进行计算
            //当高度大于限定高度,开始缩放。
            this.newHeight=this.maxHeight;
            this.newWidth=(this.srcWidth*this.maxHeight)/this.srcHeight
        }else{
            //当高度小于限定高度,直接返回原始数值。
            this.newWidth=this.srcWidth;
            this.newHeight=this.srcHeight;
        }
    }
    return [this.newWidth,this.newHeight]
}

function Simpzoom(srcWidth,srcHeight,maxWidth,nowWidth){
    var num=maxWidth*srcWidth / nowWidth;
    var _width=srcWidth*srcWidth / num;
    var _height=(srcHeight*_width) / srcWidth;
    console.log(_width,_height);
    return [_width,_height]
}

后来做项目的时候,本来一位jquery做animation的元素需要缩放:

// JavaScript Document
(function(window){
    var scaleElement = window.scaleElement = function(selector,parameter){
        return new scaleElement.fn.init(selector,parameter);
    };
    scaleElement.fn=scaleElement.prototype={
        init:function(selector,parameter){//此处的this作用域指向o.fn
            //====
            var quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/;
            var match,elem;
            match = quickExpr.exec( selector );
            elem=document.getElementById( match[2]);
             if ( elem && elem.parentNode ) {
                  this.length = 1;
                  this[0] = elem;
             }
            this.context = document;
            this.selector = selector;
            return this;
        },
       images:function (){
          var parameter=arguments||null;
          if(!parameter)return;
         // console.log(this..offsetLeft)
         new creatHtmlElement("img",this[0],{css:{position:"absolute",left:0,top:0},value:{src:parameter[0]}});
          new creatHtmlElement("img",this[0],{css:{position:"absolute",left:parameter[2]-this[0]._width,top:parameter[4],width:parameter[2],height:parameter[3]},value:{src:parameter[1]}});
       },
       hover:function (){
          var parameter=arguments||null;
          if(!parameter)return;
          var _this=this;
          var time=parameter[3]||1;
          this[0].onmouseover=function(){
             var _mask=_this.getElement(1);
             TweenMax.to(_mask,time,{css:{top:-parameter[1]+parameter[2]+5,left:0},ease:Circ.easeOut});

          }
          this[0].onmouseout=function(){
              var _mask=_this.getElement(1);
            TweenMax.to(_mask,time,{css:{top:parameter[1]-parameter[2],left:-(parameter[0]-_this[0]._width)},ease:Circ.easeOut});
          }
       },
       allHover:function (){
          var parameter=arguments||null;
            if(!parameter) return;
            if(typeof parameter[0]=="string"){
                for(var i=0;i<this.size(parameter[0]);i++){
                    var _elem=this.getElement(i,parameter[0]);
                    _elem.index=i;
                    _elem.onmouseover=function (e){
                        if(checkHover(e,this)){
                            parameter[1](this,"over");
                        }
                    }
                    _elem.onmouseout=function (e){
                        if(checkHover(e,this)){
                            parameter[1](this,"out");
                        }
                    }
                }
            }else{
                this[0].onclick=function (){
                    parameter[0](this);
                }
            }
        },
       over:function(){
           var parameter=arguments||null;
           if(!parameter)return;
           var _mask=this.getElement(1);
            TweenMax.to(_mask,2,{css:{top:parameter[1]-parameter[2],left:0},ease:Circ.easeOut});
        },
        out:function(){
             var parameter=arguments||null;
            if(!parameter)return;
             var _mask=this.getElement(1);
            TweenMax.to(_mask,2,{css:{top:-parameter[1]+parameter[2]+5,left:-(parameter[0]-this[0]._width)},ease:Circ.easeOut});
        },
      nowStatus:function (){

      },
      scale:function (srcWidth,srcHeight,maxWidth,nowWidth){
          var num=maxWidth*srcWidth / nowWidth;
          var _width=srcWidth*srcWidth / num;
          var _height=(srcHeight*_width) / srcWidth;
          this[0].style.width=_width+‘px‘;
          this[0].style.height=_height+‘px‘;
          this[0]._width=_width;

          var _img=this.getElement(0);
          _img.style.width=_width+‘px‘;
           _img.style.height=_height+‘px‘;

      },
      size:function(type){
            var _type=type||null;
            if(!_type){
                return this[0].getElementsByTagName("*").length;
            }else{
                return this[0].getElementsByTagName(type).length;
            }

        },
      getElement:function (i,type){
            var _type=type||null;
            if(!_type){
                return this[0].getElementsByTagName("*")[i];
            }else{
                return this[0].getElementsByTagName(type)[i];
            }
        }
    };
    scaleElement.fn.init.prototype = scaleElement.fn;
})(window)

然后用的时候,搞了很多数组,用来储存原始尺寸,在原始尺寸的基础上根据窗口大小等比例缩放:

ar sizeArr=[];
//var smallArr=[];The old way. Already scaled in ScaleElement.js
var titleArr=[];
var topArr=[];
var topLongArr=[];
var topShortArr=[];

var navbgArr=[];
var toplogoArr=[];

var nav0Arr=[];function windowSize(){
    /*init newArr for all*/
    newArr=getHtmlSize();//这里因为没用jquery用的是一个原生js获取窗口尺寸的数组[0]是宽,[1]是高

    /*top*/
    topArr=Simpzoom(1190,30,1190,newArr[0]);
    $("#top").css({"height":topArr[1]});
    topLongArr=Simpzoom(48,14,1050,newArr[0]);
    $(".top_0").css({"width":topLongArr[0],"height":topLongArr[1]});
    $(".top_1").css({"width":topLongArr[0],"height":topLongArr[1]});
    topShortArr=Simpzoom(24,14,1050,newArr[0]);
    $(".top_2").css({"width":topShortArr[0],"height":topShortArr[1]});

    /*top log*/
    toplogoArr=Simpzoom(256,144,1190,newArr[0]);
    $("#top_logo").css({"width":toplogoArr[0],"height":toplogoArr[1]});

    /*nav bg*/
    navbgArr=Simpzoom(1190,88,1190,newArr[0]);
    $("#nav_bg").css({"height":navbgArr[1]}); 

    /*nav butttons*/
    nav0Arr=Simpzoom(146,52,1050,newArr[0]);
    $("#nav").css({"top":(navbgArr[1]+topArr[1]-nav0Arr[1])/2+topArr[1]})
    $(".nav_0").css({"width":nav0Arr[0],"height":nav0Arr[1]});
    /*slider banner*/
    var _width=newArr[0]<1960?newArr[0]:1960;
    $(‘#box‘).css({width:_width});
    sizeArr=(new imgzoom(1840,642,newArr[0],newArr[1]).getSize());    //JS get inline style height instead of css auto sizing
    //var _banner_w=sizeArr[0]<newArr[0]?newArr[0]:sizeArr[0]+"px";
    document.getElementById("banner_0").style.height = document.getElementById("slider_blank").style.height =sizeArr[1]+"px";
     $("#slider_blank").css({"margin-top":toplogoArr[1]+topArr[1]});
     /*slider small*/
     smallArr=Simpzoom(438,152,1887,newArr[0]);
     $("#slider_small").css({"height":smallArr[1]});      

    for(var i=0; i<4; i++){
        scaleElement("#small_"+i).scale(438,152,1887,newArr[0]);
        //$(".small_"+i).css({"width":smallArr[0],"height":smallArr[1]}); The old way. Already scaled in ScaleElement.js
    }
    scaleElement("#cele_left").scale(979,687,1988,newArr[0]);
    scaleElement("#cele_right").scale(979,687,1988,newArr[0]);
    titleArr=Simpzoom(230,53,1154,newArr[0]);
    $("#celetitle").css({"width":titleArr[0],"height":titleArr[1]});
    $("#chinatitle").css({"width":titleArr[0],"height":titleArr[1]});
    $("#overseastitle").css({"width":titleArr[0],"height":titleArr[1]});

待续。。

等比缩放

时间: 2024-08-09 15:13:15

等比缩放的相关文章

UIScrollView的缩放原理

有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对其内容进行缩放处理也就是说,要完成缩放功能的话,只需要将需要缩放的内容添加到UIScrollView中 当用户在UIScrollView身上使用捏合手势时,UIScrollView会给代理发送一条消息,询问代理究竟要缩放自己内部的哪一个子控件(哪一块内容) 当用户在UIScrollView身上使用捏合手势时,UIScrollView会调用代理的viewForZoomingInScroll

自定义视图之————安卓图库缩放拖拽的完整实现

加了大部分注释,看注释应该可以明白基本的思路.欢迎大神留言拍砖,此文适合小白观看. package com.example.imagedeal; import android.content.Context; import android.graphics.Matrix; import android.graphics.RectF; import android.graphics.drawable.Drawable; import android.util.AttributeSet; impor

图像处理---关于像素坐标矩阵变换(平移,旋转,缩放,错切)

介绍 坐标变换矩阵是一个3*3的矩阵,用来对图形进行坐标变化,将原来的坐标点转移到新的坐标点,因为一个图片是有点阵和每一点上的颜色信息组成的,所以对坐标的变换,就是对每一点进行搬移形成新的图片.具体的说图形的放大缩小,移动,旋转,透视,扭曲这些效果都可以用此矩阵来完成. 平移 旋转 绕原点逆时针旋转θ度角的变换公式是 x' = xcosθ ? ysinθ 与 y.' = xsinθ + ycosθ 缩放 错切 其他 Demo 见代码:http://download.csdn.net/detail

jQuery实现等比例缩放大图片

在页面布局时,有时会遇到大图片将页面容器撑大,超出规定区域, 这时我们就需要将图片按比例缩放,让大图片自适应页面布局. 查看演示http://itmyhome.com/jquery_image_scaling/ 1.页面中有如下图片 <img alt="leaf" src="img/leaf.jpg"> 2.使用jQuery将图片缩放 <script type="text/javascript"> window.onloa

Android多点触控技术实战,自由地对图片进行缩放和移动

转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/11100327 在上一篇文章中我带着大家一起实现了Android瀑布流照片墙的效果,虽然这种效果很炫很酷,但其实还只能算是一个半成品,因为照片墙中所有的图片都是只能看不能点的.因此本篇文章中,我们就来对这一功能进行完善,加入点击图片就能浏览大图的功能,并且在浏览大图的时候还可以通过多点触控的方式对图片进行缩放. 如果你还没有看过 Android瀑布流照片墙实现,体验不规则排列的美感

css 布局之定位 相对/绝对/成比例缩放

给body添加 overflow: hidden; 可以将页面所有的 滚动条隐藏,但必须要给body 设置一个高度 overflow: hidden; height:864px; 父元素必须要设置 position:relative 必须设置 width 与 height 且不能用百分比 父层如果是图片,要使用  background: url(bg.jpg) no-repeat; 子元素必须设置 position: absolute; 必须设置 width 与 height 定位使用 top

图像缩放示例

二维图像的缩放属于仿射变换或者透视变换的范畴,一般可以通过OpenCV的warpAffine()或者warpPerspective()函数实现. 出于兴趣,根据仿射变换公式自己简单写了一个函数实现图像的缩放,缩放中心设置为图像中心. 代码如下: 1 #include <iostream> 2 #include <string> 3 #include <opencv2/opencv.hpp> 4 5 using namespace std; 6 using namespa

Android绘画板(普通绘画模式和缩放平移绘画模式)

ScaleSketchPadDemo 项目地址: demo apk体验下载 demo2 apk体验下载 用法: 进入项目根目录:https://github.com/ShaunSheep/ScaleSketchPadDemo clone or download   项目到本地, 打开Android Studio->file->new->import new moudle->选中本地的app或aap2 此项目包含两个模块 app1 为普通绘画板 app2 为可所发的绘画板 方便各位A

百度地图API一:根据标注点坐标范围计算显示缩放级别zoom自适应显示地图

百度地图中根据页面中的point,自动设置缩放级别和视图中心,将所有的point在视图范围内展示. var points = [point1, point2,point3]; var view = map.getViewport(eval(points)); var mapZoom = view.zoom; var centerPoint = view.center; map.centerAndZoom(centerPoint,mapZoom);

java处理图片--图片的缩放,旋转和马赛克化

这是我自己结合网上的一些资料封装的java图片处理类,支持图片的缩放,旋转,马赛克化.(转载请注明出处:http://blog.csdn.net/u012116457) 不多说,上代码: package deal; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.geom.AffineTransform; impo