欢迎光临
我们一直在努力

网站JS源码2--DIV伸缩控件

 

实现原理原理大家都知道,就是改变DIV长宽(UL,LI等标签都可以),技术含量在于就是怎么样去控件这个过程。

放上我的核心代码,可是自己花了几天写的呵呵,本人太懒,不喜欢写注释,下面都是临时加上的^^

 

var Flex = {};
Flex 
= Class.create();
Flex.prototype 
= {
  initialize: 
function(options) {
    
this.options = {
      cont: 
''//要改变大小的DIV。。
      speed: 0//速率
      space: [], //每毫秒改变象素大小如[0, 10],0指(宽)不改变,10指(高)
      wcomp0//(宽)总共伸缩大小,0为不改变,可以为负值
      hcomp0 //(高)总共伸缩大小,0为不改变
    };
    Object.extend(
this.options, options || {}); 
    
this.cont = this.options.cont;
    
this.speed = this.options.speed;
    
this.space = this.options.space;
    
this.wcomp = this.options.wcomp;
    
this.hcomp = this.options.hcomp;
    
    
this.c_wcomp = Math.abs(this.wcomp);
    
this.c_hcomp = Math.abs(this.hcomp);
    
//上面代码为什么这样写?如this.wcomp = this.options.wcomp;,就是为了下文调用this.wcomp显得幽雅哈哈
    
    
this.play();
  },
  
  play: 
function() {
    
if(this.wcomp == 0 && this.hcomp == 0){
      
return;
    }
    
    
var Speed = this.speed, Space, num;
    
    
//宽度缩小
    if (this.wcomp != 0) {
    Space 
= this.space[0];
    
if(Math.abs(this.wcomp) < this.c_wcomp / 5){
      Space 
=  Math.round(Math.abs(this.wcomp / 5));
      
if (Space > this.space[0]) Space = this.space[0];
      
if(Space < 1) Space = 1;
    }
    
    
if (this.wcomp < 0) {    
      
if(this.wcomp < -Space){
        
this.wcomp += Space;
        num 
= Space;
      } 
else {
        num 
= -this.wcomp;
        
this.wcomp = 0;
      }

      this.setWidth(-num);
    } 
else {
      
if(this.wcomp > Space){
        
this.wcomp -= Space;
        num 
= Space;
      } 
else {
        num 
= this.wcomp;
        
this.wcomp = 0;
      }

      this.setWidth(num);
    }}
    
    
//高度缩小
    if (this.hcomp != 0) {
    Space 
= this.space[1];
    
if(Math.abs(this.hcomp) < this.c_hcomp / 5){
      Space 
=  Math.round(Math.abs(this.hcomp / 5));
      
if (Space > this.space[1]) Space = this.space[1];
      
if(Space < 1) Space = 1;
    }    
    
    
if (this.hcomp < 0) {    
      
if(this.hcomp < -Space){
        
this.hcomp += Space;
        num 
= Space;
      } 
else {
        num 
= -this.hcomp;
        
this.hcomp = 0;
      }

      this.setHeight(-num);
    } 
else {
      
if(this.hcomp > Space){
        
this.hcomp -= Space;
        num 
= Space;
      } 
else {
        num 
= this.hcomp;
        
this.hcomp = 0;
      }

      this.setHeight(num);
    }}
    
    setTimeout(
this.play.bind(this), Speed);
  },
  
  setWidth: 
function(num) {
    
this.cont.style.width = Math.abs(this.cont.style.width.replace('px''')) + num + 'px';
  },
  
  setHeight: 
function(num) {
    
this.cont.style.height = Math.abs(this.cont.style.height.replace('px''')) + num + 'px';
  }
};

 

再看看怎么调用的吧

 

//分类DIV伸缩
function FlexClass(box, e) {
  box 
= $(box);
  
var comp = -44;
  
if (Math.abs(box.style.height.replace('px''')) < 30) {
    comp 
= Math.abs(comp);
    e.className 
= 'jian'
  } 
else {
    e.className 
= 'jian1'
  }

  var options = {
    cont: box,
    speed: 
10,
    space: [
010],
    wcomp: 
0,
    hcomp: comp
  };
  
new Flex(options);
}

 

大家可以看到这里的,总改变长宽wcomp与hcomp我们可以动态计算来实现我们的效果,到底是隐藏还是显示,都由我们自己来控制。

 

  • 海报
海报图正在生成中...
赞(0) 打赏
声明:
1、本博客不从事任何主机及服务器租赁业务,不参与任何交易,也绝非中介。博客内容仅记录博主个人感兴趣的服务器测评结果及一些服务器相关的优惠活动,信息均摘自网络或来自服务商主动提供;所以对本博客提及的内容不作直接、间接、法定、约定的保证,博客内容也不具备任何参考价值及引导作用,访问者需自行甄别。
2、访问本博客请务必遵守有关互联网的相关法律、规定与规则;不能利用本博客所提及的内容从事任何违法、违规操作;否则造成的一切后果由访问者自行承担。
3、未成年人及不能独立承担法律责任的个人及群体请勿访问本博客。
4、一旦您访问本博客,即表示您已经知晓并接受了以上声明通告。
文章名称:《网站JS源码2--DIV伸缩控件》
文章链接:https://www.456zj.com/9040.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址