1+ $ . fn . cndkbeforeafter = function ( options ) {
2+
3+ // Default settings
4+ var settings = $ . extend ( {
5+ mode : "hover" , /* hover,drag */
6+ showText : true ,
7+ beforeText : "BEFORE" ,
8+ afterText : "AFTER" ,
9+ seperatorWidth : "4px" ,
10+ seperatorColor : "#ffffff" ,
11+ hoverEffect : true ,
12+ } , options ) ;
13+
14+ // One or multlple
15+ if ( this . length > 0 )
16+ {
17+ this . each ( function ( ) {
18+
19+ // Get contents
20+ var count = $ ( this ) . find ( ">div" ) . length ;
21+ if ( count <= 1 )
22+ {
23+ // No images
24+ console . log ( "No before-after images found." ) ;
25+ }
26+
27+ // Continue
28+ var root = $ ( this ) ;
29+ root . addClass ( "cndkbeforeafter cndkbeforeafter-root" ) ;
30+ root . append ( "<div class='cndkbeforeafter-seperator' style='width:" + settings . seperatorWidth + " ;background:" + settings . seperatorColor + "'></div>" ) ;
31+
32+ // Container
33+ root . append ( "<div class='cndkbeforeafter-container'></div>" ) ;
34+
35+ // Hover Effect
36+ if ( settings . hoverEffect == true )
37+ {
38+ root . addClass ( "cndkbeforeafter-hover" ) ;
39+ }
40+
41+ // Before-After text
42+ if ( settings . showText == true )
43+ {
44+ root . append ( "<div class='cndkbeforeafter-item-before-text'>" + settings . beforeText + "</div>" ) ;
45+ root . append ( "<div class='cndkbeforeafter-item-after-text'>" + settings . afterText + "</div>" ) ;
46+ }
47+
48+ for ( i = 0 ; i < count ; i ++ )
49+ {
50+ // Before
51+ var div1 = $ ( this ) . find ( ">div" ) . eq ( i ) . find ( 'div[data-type="before"]' ) ;
52+ var img1 = $ ( this ) . find ( ">div" ) . eq ( i ) . find ( 'div[data-type="before"] img' ) ;
53+ img1 . addClass ( "cndkbeforeafter-item-before" ) ;
54+ div1 . addClass ( "cndkbeforeafter-item-before-c" ) ;
55+ div1 . css ( "overflow" , "hidden" ) ;
56+ div1 . css ( "z-index" , "2" ) ;
57+
58+ // After
59+ var div2 = $ ( this ) . find ( ">div" ) . eq ( i ) . find ( 'div[data-type="after"]' ) ;
60+ var img2 = $ ( this ) . find ( ">div" ) . eq ( i ) . find ( 'div[data-type="after"] img' ) ;
61+ img2 . addClass ( "cndkbeforeafter-item-after" ) ;
62+ div2 . addClass ( "cndkbeforeafter-item-after-c" ) ;
63+ div2 . css ( "z-index" , "1" ) ;
64+
65+
66+ // Image-Item width/height
67+ var itemwidth = img1 . width ( ) ;
68+ var itemheight = img1 . height ( ) ;
69+
70+ // Screen width
71+ var screenWidth = $ ( this ) . parent ( ) . width ( ) ;
72+ if ( screenWidth < itemwidth )
73+ {
74+ itemheight = itemheight / ( itemwidth / screenWidth ) ;
75+ itemwidth = screenWidth ;
76+ img1 . css ( "width" , itemwidth + "px" ) ;
77+ img2 . css ( "width" , itemwidth + "px" ) ;
78+ }
79+
80+ // Item
81+ $ ( this ) . find ( ">div" ) . eq ( i ) . addClass ( "cndkbeforeafter-item" ) ;
82+ $ ( this ) . find ( ">div" ) . eq ( i ) . css ( "height" , itemheight + "px" ) ;
83+
84+ // Start position
85+ div1 . css ( "width" , "50%" ) ;
86+ div2 . css ( "width" , "50%" ) ;
87+ $ ( ".cndkbeforeafter-seperator" ) . css ( "left" , "50%" ) ;
88+
89+ // Root inline
90+ root . css ( "width" , itemwidth + "px" ) ;
91+ root . css ( "height" , itemheight + "px" ) ;
92+ }
93+
94+ if ( settings . mode == "hover" )
95+ {
96+ $ ( ".cndkbeforeafter-seperator, .cndkbeforeafter-item > div" ) . addClass ( "cndkbeforeafter-hover-transition" ) ;
97+ $ ( root ) . mousemove ( function ( e ) {
98+ var parentOffset = $ ( this ) . parent ( ) . offset ( ) ;
99+ var mouseX = parseInt ( ( e . pageX - parentOffset . left ) ) ;
100+ var mousePercent = ( mouseX * 100 ) / parseInt ( root . width ( ) ) ;
101+ $ ( this ) . find ( ".cndkbeforeafter-item-before-c" ) . css ( "width" , mousePercent + "%" ) ;
102+ $ ( this ) . find ( ".cndkbeforeafter-item-after-c" ) . css ( "width" , ( 100 - mousePercent ) + "%" ) ;
103+ $ ( this ) . find ( ".cndkbeforeafter-seperator" ) . css ( "left" , mousePercent + "%" ) ;
104+ } ) . mouseleave ( function ( ) {
105+ $ ( this ) . find ( ".cndkbeforeafter-item-after-c" ) . css ( "width" , "50%" ) ;
106+ $ ( this ) . find ( ".cndkbeforeafter-item-before-c" ) . css ( "width" , "50%" ) ;
107+ $ ( this ) . find ( ".cndkbeforeafter-seperator" ) . css ( "left" , "50%" ) ;
108+ } ) ;
109+ }
110+ else if ( settings . mode == "drag" )
111+ {
112+ $ ( ".cndkbeforeafter-seperator, .cndkbeforeafter-item > div" ) . addClass ( "cndkbeforeafter-drag-transition" ) ;
113+ $ ( ".cndkbeforeafter-seperator" ) . draggable ( ) ;
114+ $ ( root ) . click ( function ( e ) {
115+ var parentOffset = $ ( this ) . parent ( ) . offset ( ) ;
116+ var mouseX = parseInt ( ( e . pageX - parentOffset . left ) ) ;
117+ var mousePercent = ( mouseX * 100 ) / parseInt ( root . width ( ) ) ;
118+ $ ( this ) . find ( ".cndkbeforeafter-item-after-c" ) . css ( "width" , mousePercent + "%" ) ;
119+ $ ( this ) . find ( ".cndkbeforeafter-item-before-c" ) . css ( "width" , ( 100 - mousePercent ) + "%" ) ;
120+ $ ( this ) . find ( ".cndkbeforeafter-seperator" ) . css ( "left" , mousePercent + "%" ) ;
121+ } ) ;
122+ }
123+
124+ $ ( window ) . resize ( function ( ) {
125+
126+ } ) ;
127+ } ) ;
128+ }
129+ } ;
0 commit comments