/**
* 已知图片的宽度和高度的等比例缩放
*/
function knowImgSize(id) {
var idWidth = $(id).width(), // 容器的宽度和高度
idHeight = $(id).height();
$(id + ' img').each(function(index,img){
var img_w = $(this).width(),
img_h = $(this).height();
// 如果图片自身宽度大于容器的宽度的话 那么高度等比例缩放
if(img_w > idWidth) {
var height = img_h * idWidth / img_w;
$(this).css({"width":idWidth, "height":height});
}
});
}
// 初始化
$(function(){
knowImgSize("#demo1");
});
复制代码
js实现放大缩小页面
在firefox,chrom,ie11测试过可以,其它未测
<script type="text/javascript">
var size = 1.0;function zoomout() {
size = size + 0.1;
set();
}
function zoomin() {
size = size - 0.1;
set();
}
function set() {
//document.body.style.cssText = document.body.style.cssText + '; -webkit-transform: scale(' + size + ');-webkit-transform-origin: 0 0;';
//document.body.style.cssText = document.body.style.cssText + '; -webkit-transform: scale(' + size + '); ';
//$(body).css("width","120%);
document.body.style.zoom = size;
document.body.style.cssText += '; -moz-transform: scale(' + size + ');-moz-transform-origin: 0 0; '; //
}
</script>
- /*
- * 缩放函数zoomIt,它实际缩放的是el的内部元素
- * @param {htmlElemnt} el 缩放目标htmlElement
- * @param {Number} xScale x方向缩放比例
- * @param {Number} yScale y方向缩放比例
- */
- zoomIt : function(el, xScale, yScale) {
- var S = KISSY,
- style = $(el).attr('style') || "";
- if(S.UA['firefox']) {
- // x轴方向和y方向分别缩放的比例
- $(el).css('transform', 'scale(' + xScale + ', ' + yScale + ')');
- // 缩放后,相对于父元素左上角的偏移量
- $(el).css('transform-origin', '0px 0px');
- }
- else if(S.UA['ie'] >= 9) {
- $(el).css('-ms-transform', 'scale(' + xScale + ')');
- $(el).css('-ms-transform-origin', '0px 0px');
- }
- else if(S.UA['ie'] < 9) {
- $(el).css('zoom', xScale);
- }
- else {
- $(el).css('-webkit-transform', 'scale(' + xScale + ', ' + yScale + ')');
- $(el).css('-webkit-transform-origin', '0px 0px');
- }
- }
在浏览器的判断是,这里使用了KISSY;在属性操作上,这里用JQ. 读者可以根据自己的习惯进行相应的修改。
$(document).ready(function(){
var num=0;
$('#button').click(function(e){
if(num++ %2 == 0){
$('#part2').height($(document).height() - 20);
document.getElementById("part2").style.float="right";
document.getElementById("part2").style.left=0;
document.getElementById("part2").style.zIndex="2";
document.getElementById("part3").style.zIndex="1";
$('#button').html('↘↙');
$("#gridOrg").data("kendoGrid").refresh();
$("#gridWorkMonth").data("kendoGrid").refresh();
}else{
$('#part2').height($('#fade').height() - $('#part3').height()-20);
document.getElementById("part2").style.left='305px';
$('#button').html('↖↗');
$("#gridOrg").data("kendoGrid").refresh();
$("#gridWorkMonth").data("kendoGrid").refresh();
}
});
});