Results 1 to 5 of 5

Thread: Before and After image viewer

  1. #1
    Join Date
    Mar 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Before and After image viewer

    Hi all: A colleague of mine pointed out this really neat before-and-after image effect on this ABC news site: http://www.abc.net.au/news/events/ja...eforeafter.htm We were wondering if anyone out there has a script that can do something like this. Thanks!

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    I will tidy this tomorrow

    but

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    
    <style type="text/css">
    /*<![CDATA[*/
    
    #tst {
     position:relative;width:940px;height:529px;border:solid black 1px;
    }
    
    .before {
      position:relative;width:940px;height:529px;background-Image:url(http://www.abc.net.au/news/events/japan-quake-2011/images/beforeafter/sendai-arahama-1.jpg);background-Repeat:none;
    }
    
    .after {
      position:absolute;left:936px;top:0px;width:0px;height:529px;background-Image:url(http://www.abc.net.au/news/events/japan-quake-2011/images/beforeafter/sendai-arahama-2.jpg);background-Repeat:none;
      border-Left:solid red 4px;
    }
    
    /*]]>*/
    </style>
    
    <script type="text/javascript">
    /*<![CDATA[*/
    // Drag & Drop III (05-Febuary-2009)
    // by Vic Phillips http://www.vicsjavascripts.org.uk/
    
    // obj.drag=new zxcDrag(document.getElementById('tst'),false,'xy',[0,300,0,250])
    // where:
    //  parameter 1 = the object to activate drag on mousedown.                                (object)
    //  parameter 2 = the object to dragged on mousemove.                                      (object, or false to default to parameter 1)
    //  parameter 3 = (optional) a string containing 'x' to drag x and/or 'y' to drag y.       (string, default 'xy')
    //  parameter 4 = (optional) an array defining the minimum and maximum x and y coordinates (array, default [])
    //                 where:
    //                 field 0 = the minimum x cordinate. (digits, null = not set)
    //                 field 1 = the maximum x cordinate. (digits, null = not set)
    //                 field 2 = the minimum y cordinate. (digits, null = not set)
    //                 field 3 = the maximum y cordinate. (digits, null = not set)
    //  parameter 4 = (optional) a string defining a function to be called on mouseup.         (string) (see Note 2)
    //  parameter 5 = (optional) a string defining a function to be called on mousemove.       (string) (see Note 2)
    //  parameter 6 = (optional) a string defining a function to be called on mousedown.       (string) (see Note 2)
    
    // Note 1:
    //  On mousedown of the object the object z_index is increased by 10.
    //  On the mouseup the z-Index is restored to the original z-Index.
    
    // Note 2:
    //  When a function is called the zxcDrag function is passed to the function.
    //  The drag object may be access by argument[0].dobj, and the event as argument[1].
    //  e.g.
    //   function Test(obj,evt){
    //    alert(obj.dobj);
    //    alert(evt.type);
    //   }
    
    
    // Functional Code Size 2.43K
    
    // Functional Code
    
    function zxcDrag(obj,root,dxy,mm,uf,mf,df){
     this.dobj=root?root:obj;
     mm=mm||['x','x','y','y'];
     dxy=(dxy||'xy').toLowerCase();
     this.dx=dxy.indexOf('x')>-1?true:false;
     this.dy=dxy.indexOf('y')>-1?true:false;
     this.minX=typeof mm[0]=='number'&&this.dx?mm[0]+.1:false;
     this.maxX=typeof mm[1]=='number'&&this.dx?mm[1]+.1:false;
     this.minY=typeof mm[2]=='number'&&this.dy?mm[2]+.1:false;
     this.maxY=typeof mm[3]=='number'&&this.dy?mm[3]+.1:false;
     this.df=df||''; this.mf=mf||''; this.uf=uf||'';
     this.addevt(obj,'mousedown','down');
     this.addevt(document,'mousemove','move');
     this.addevt(document,'mouseup','up');
    }
    
    zxcDrag.prototype.down=function(ev){
     document.onselectstart=function(event){ window.event.returnValue=false; }
     this.dobj.style.zIndex=zxcLT(this.dobj,'z-Index')+10+'';
     this.lastXY=[ev.clientX,ev.clientY];
     this.drag=true;
     if (window[this.df]) window[this.df](this,ev);
     if (!window.event) ev.preventDefault();
     return false;
    }
    
    zxcDrag.prototype.move=function(ev){
     if (this.drag){
      var mse=[ev.clientX,ev.clientY];
      var lft=zxcLT(this.dobj,'left')+mse[0]-this.lastXY[0];
      var top=zxcLT(this.dobj,'top')+mse[1]-this.lastXY[1];
      if (this.dx) this.dobj.style.left=Math.min(this.maxX||lft,Math.max(this.minX||lft,lft))+'px';
      if (this.dy) this.dobj.style.top=Math.min(this.max1||top,Math.max(this.minY||top,top))+'px';
      lft=zxcLT(this.dobj,'left');
      top=zxcLT(this.dobj,'top');
      if ((this.maxX&&lft>=this.maxX-.1)||(this.minX&&lft<=this.minX-.1)||(this.maxY&&top>=this.maxY-.1)||(this.minY&&top<=this.minY-.1)) this.drag=false;
      this.lastXY=mse;
      if (this.mf) this.mf(this,ev);
     }
     if (!window.event) ev.preventDefault();
     return false;
    }
    
    zxcDrag.prototype.up=function(ev){
     if (this.drag){
      this.drag=false;
      document.onselectstart=null;
      this.dobj.style.zIndex=zxcLT(this.dobj,'z-Index')-10+'';
      if (window[this.uf]) window[this.uf](this,ev);
     }
    }
    
    zxcDrag.prototype.addevt=function(o,t,f){
     var oop=this;
     if (o.addEventListener) o.addEventListener(t,function(e){ return oop[f](e);}, false);
     else if (o.attachEvent) o.attachEvent('on'+t,function(e){ return oop[f](e); });
     else {
      var prev=o['on'+t];
      if (prev) o['on'+t]=function(e){ prev(e); oop[f](e); };
      else o['on'+t]=o[f];
     }
    }
    
    function zxcLT(obj,p){
     if (obj.currentStyle) return parseInt(obj.currentStyle[p.replace(/-/g,'')]);
     return parseInt(document.defaultView.getComputedStyle(obj,null).getPropertyValue(p));
    }
    
    /*]]>*/
    </script>
    
    </head>
    
    
    
    </head>
    
    <body>
     <div id="tst" >
      <div class="before" ></div>
      <div class="after" ></div>
     </div>
    
    <script type="text/javascript">
    /*<![CDATA[*/
    
    function zxcBeforeAfter(o){
     var oop=this,obj=document.getElementById(o.ID),after=obj.getElementsByTagName('DIV')[1];
     this.drag=new zxcDrag(after,null,'x',[0,this.width],null,oop.drag)
     this.drag.after=after;
     this.drag.width=obj.offsetWidth;
    }
    
    zxcBeforeAfter.prototype={
    
     drag:function(){
      this.dobj.style.width=Math.max(this.width-this.dobj.offsetLeft,0)+'px';
      this.dobj.style.backgroundPosition=-this.dobj.offsetLeft+'px';
     }
    }
    
    new zxcBeforeAfter({
     ID:'tst'
    });
    
    /*]]>*/
    </script>
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Mar 2011
    Location
    Lagos Nigeria
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Have you checked any equivalent script type on DD page?

  4. #4
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    
    <style type="text/css">
    /*<![CDATA[*/
    
    #tst {
     position:relative;width:940px;height:529px;border:solid black 1px;
    }
    
    .before {
      position:relative;width:940px;height:529px;background-Image:url(http://www.abc.net.au/news/events/japan-quake-2011/images/beforeafter/sendai-arahama-1.jpg);background-Repeat:none;
    }
    
    .after {
      position:absolute;left:936px;top:0px;width:0px;height:529px;background-Image:url(http://www.abc.net.au/news/events/japan-quake-2011/images/beforeafter/sendai-arahama-2.jpg);background-Repeat:none;
      border-Left:solid red 4px;cursor:move;
    }
    
    /*]]>*/
    </style>
    
    </head>
    
    
    
    </head>
    
    <body>
     <div id="tst" >
      <div class="before" ></div>
      <div class="after" ></div>
     </div>
    
    
    <script type="text/javascript">
    /*<![CDATA[*/
    // Before and After Images (18-March-2011)
    // by Vic Phillips http://www.vicsjavascripts.org.uk/
    
    //****** Functional Code(1.95K) - NO NEED to Change.
    
    function zxcBeforeAfter(o){
     var oop=this,obj=document.getElementById(o.ID),after=obj.getElementsByTagName('DIV')[1],max=this.style(obj,'width')-this.style(after,'border-Left-Width'),mde=o.Mode;
     this.mde=typeof(mde)=='string'&&mde.charAt(0).toUpperCase=='V'?['top','height','Bottom',1]:['left','width','Left',0];
     max=this.style(obj,this.mde[1])-this.style(after,'border-'+this.mde[2]+'-Width');
     after.style.left=max+'px';
     after.style.zIndex='101';
     after.style[this.mde[1]]='0px';
     this.after=after;
     this.min=0;
     this.max=max;
     this.lstmse=max;
     this.drag=false;
     this.addevt(obj,'mousedown','down');
     this.addevt(document,'mousemove','move');
     this.addevt(document,'mouseup','up');
    }
    
    zxcBeforeAfter.prototype={
    
     slide:function(pos){
      this.after.style[this.mde[0]]=Math.min(Math.max(pos,this.min),this.max)+'px';
      this.after.style[this.mde[1]]=Math.max(this.max-pos,0)+'px';
      this.after.style.backgroundPosition=-pos+'px';
     },
    
     down:function(ev){
      document.onselectstart=function(event){ window.event.returnValue=false; }
      this.lstmse=[ev.clientX,ev.clientY][this.mde[3]];
      this.drag=true;
      if (!window.event) ev.preventDefault();
      return false;
     },
    
     move:function(ev){
      if (this.drag){
       var mse=[ev.clientX,ev.clientY][this.mde[3]];
       var pos=this.style(this.after,this.mde[0])+mse-this.lstmse;
       this.lstmse=mse;
       this.slide(pos);
      }
      if (!window.event) ev.preventDefault();
      return false;
     },
    
     up:function(ev){
      if (this.drag){
       this.drag=false;
       document.onselectstart=null;
      }
     },
    
     addevt:function(o,t,f,p){
      var oop=this;
      if (o.addEventListener) o.addEventListener(t,function(e){ return oop[f](e,p);}, false);
      else if (o.attachEvent) o.attachEvent('on'+t,function(e){ return oop[f](e,p); });
     },
    
     style:function(obj,p){
     if (obj.currentStyle) return parseInt(obj.currentStyle[p.replace(/-/g,'')]);
     return parseInt(document.defaultView.getComputedStyle(obj,null).getPropertyValue(p));
    }
    
    
    }
    
    new zxcBeforeAfter({
     ID:'tst',
     Mode:'Horizontal'
    });
    
    /*]]>*/
    </script>
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  5. #5
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •