By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
LahbabiGuideLahbabiGuide
  • الرئيسية
  • مزيج مِن أفكاري
    • مقالاتي
    • قصص قصيرة
    • سيناريوهات أفلام قصيرة
    • نصوص مسرحية قصيرة
  • أفكار المشاريع
  • قصص النجاح
  • أخبار تقنية
  • القرأن الكريم
Search
  • Advertise
© 2022 Zakariaelahbabi.com . Company. All Rights Reserved.
Reading: Image Comparison Slide Using jQuery and CSS | Image Compare Slider
Share
Notification Show More
Latest News
bootstrap-clean-buttons-with-hover-effect-|-plain-button-ui-kit
Bootstrap Clean Buttons With Hover Effect | Plain Button UI Kit
Bootstrap
bootstrap-datatable-with-sort,-pagination,-and-search-|-sorting-data-table
Bootstrap Datatable With Sort, Pagination, and Search | Sorting Data Table
Bootstrap
upload-image-in-php-with-mysql-database-|-web-dev-trick
Upload Image In PHP With MySQL Database | Web Dev Trick
PHP
bootstrap-tooltip-progress-bar-animation-|-percentage-values-in-tooltip
Bootstrap Tooltip Progress Bar Animation | Percentage Values in Tooltip
Bootstrap
php-age-calculator-program-|-calculate-age-in-php
PHP Age Calculator Program | Calculate Age In PHP
PHP
Aa
LahbabiGuideLahbabiGuide
Aa
  • مزيج مِن أفكاري
  • أخبار تقنية
  • تطوير الويب
  • أفكار المشاريع
Search
  • Home
    • Home 1
    • Default Home 2
    • Default Home 3
    • Default Home 4
    • Default Home 5
  • Categories
    • تطوير الويب
    • أخبار تقنية
    • أفكار المشاريع
    • مزيج مِن أفكاري
    • قصص النجاح
  • Bookmarks
  • More Foxiz
    • Sitemap
Follow US
  • Advertise
© 2022 Foxiz News Network. Ruby Design Company. All Rights Reserved.
image-comparison-slide-using-jquery-and-css-|-image-compare-slider
LahbabiGuide > تطوير الويب > CSS > Image Comparison Slide Using jQuery and CSS | Image Compare Slider
CSS

Image Comparison Slide Using jQuery and CSS | Image Compare Slider

admin
Last updated: 2023/03/11 at 7:35 صباحًا
By admin
Share
19 Min Read
SHARE

How we can create a compare slider for the image using JS or any JS library? Solution: Check out this Image Comparison Slide Using jQuery and CSS, Image Compare Slider.

Previously I have shared a Before and After Image slider, this is also similar to that but it is an advanced one and it’s based on jQuery. Basically, an image comparison slider used for comparing two images using a splitter slide handle. It helps to compare before/after or original/modified images easily. This type of compare slider used on editing websites and image compress websites.

Today you will learn to create an Image Compare Slider. Basically, there is a box with two images, a slider handle, and some texts. Middle of the box, there is placed the slider handle for drag and slide. On the left side this is a modified image with text “modified” and on the right side is the original image with text “original”. When when will drag handle and start slide then on part’s width will increase according to your slider direction.

So, Today I am sharing Image Comparison Slide Using jQuery and CSS. There I have used CSS for styling and jQuery for functioning, also jQuery modified CSS values to execute the result on the action. This is a complete program and ready to use on any kind of website. You can use this program after image modification or backend integration.

If you are thinking now how this image compares slider actually is, then see the preview given below.

Preview Of Image Compare Slider

See this video preview to getting an idea of how this comparing slider looks like.

https://webdevtrick.com/wp-content/uploads/image-comparison-slider.mp4

Live Demo

Now you can see this program visually, Also you can see it live by pressing the button given above. If you like this, then get the source code of its.

You May Also Like:

  • Video Player For Website
  • Image Upload With Preview
  • Swipe Testimonial Slider
  • HTML CSS Image Lightbox

Before sharing source code, let’s talk about it. First I have created a figure using HTML <figure> tag and placed all elements inside the tag. Inside the figure tag, I have placed an IMG tag for the original image, a span for text, a div with span for modified image and text, and another span to place the handle. Also in the HTML file, I have linked other files like CSS, JS, and jQuery CDN.

Now using CSS I have placed all the items in the right place, as you can see in the preview. For placing the second modified image and the slider handle I have used CSS background–image: command and gave IMG URL. There I have used transition command for smooth animation effects. And used CSS @keyframe command to create the animation effects.

jQuery handling here all features of the program by modifying HTML and CSS values dynamically. I have used HTML Data-* attribute to store custom data and pass in the JS file. There I have used JS if{} else{} commands and adding/removing CSS class names according to action. Also, I have used jQuery dragElement and resizeElement commands to create the program.

Left all other things you will understand after getting the codes, I can’t explain all in writing. For creating this program you have to create 3 files. First file for HTML, second file for CSS, and the third file for JavaScript. Follow the steps to creating this program without any error.

index.html

Create an HTML file named ‘index.html‘ and put these codes given here.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

lang=“en” >

  charset=“UTF-8”>

  </span><span>Image Comparison Slider | Webdevtrick.com</span><span>

  

  href=‘https://fonts.googleapis.com/css?family=Open+Sans:400,300,700’ rel=‘stylesheet’ type=‘text/css’>

rel=“stylesheet” href=“https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css”>

rel=“stylesheet” href=“./style.css”>

class=“image-container”>

src=“https://webdevtrick.com/wp-content/uploads/skynature.jpg” alt=“Original Image”>

class=“image-label” data-type=“original”>Original

class=“resize-image”>

class=“image-label” data-type=“modified”>Modified

class=“handle”>

  

  

style.css

Now create a CSS file named ‘style.css‘ and put these codes given here.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

/* Code By Webdevtrick ( https://webdevtrick.com ) */

*, *:after, *:before {

  -webkit-box-sizing: border-box;

  -moz-box-sizing: border-box;

  box-sizing: border-box;

}

body {

  font-size: 100%;

  font-family: “Open Sans”, sans-serif;

  color: #445b7c;

  background-color: #445b7c;

}

a {

  color: #dc717d;

  text-decoration: none;

}

img {

  max-width: 100%;

}

header {

  position: relative;

  height: 160px;

  line-height: 160px;

  text-align: center;

}

header h1 {

  font-size: 22px;

  font-size: 1.375rem;

  color: #ffffff;

  font-weight: 300;

  -webkit-font-smoothing: antialiased;

  -moz-osx-font-smoothing: grayscale;

}

@media only screen and (min-width: 768px) {

  header {

    height: 240px;

    line-height: 240px;

  }

  header h1 {

    font-size: 32px;

    font-size: 2rem;

  }

}

.image-container {

  position: relative;

  width: 90%;

  max-width: 768px;

  margin: 2em auto;

}

.image-container img {

  display: block;

}

@media only screen and (min-width: 768px) {

  .image-container {

    margin: 4em auto;

  }

}

.image-label {

  position: absolute;

  bottom: 0;

  right: 0;

  color: #ffffff;

  padding: 1em;

  -webkit-font-smoothing: antialiased;

  -moz-osx-font-smoothing: grayscale;

  opacity: 0;

  -webkit-transform: translateY(20px);

  -moz-transform: translateY(20px);

  -ms-transform: translateY(20px);

  -o-transform: translateY(20px);

  transform: translateY(20px);

  -webkit-transition: -webkit-transform 0.3s 0.7s, opacity 0.3s 0.7s;

  -moz-transition: -moz-transform 0.3s 0.7s, opacity 0.3s 0.7s;

  transition: transform 0.3s 0.7s, opacity 0.3s 0.7s;

}

.image-label.is-hidden {

  visibility: hidden;

}

.is-visible .image-label {

  opacity: 1;

  -webkit-transform: translateY(0);

  -moz-transform: translateY(0);

  -ms-transform: translateY(0);

  -o-transform: translateY(0);

  transform: translateY(0);

}

.resize-image {

  position: absolute;

  top: 0;

  left: 0;

  width: 0;

  height: 100%;

  overflow: hidden;

  background: url(“https://webdevtrick.com/wp-content/uploads/skynaturemod.jpg”) no-repeat left top;

  background-size: auto 100%;

  -webkit-transform: translateZ(0);

  -moz-transform: translateZ(0);

  -ms-transform: translateZ(0);

  -o-transform: translateZ(0);

  transform: translateZ(0);

  -webkit-backface-visibility: hidden;

  backface-visibility: hidden;

}

.resize-image .image-label {

  right: auto;

  left: 0;

}

.is-visible .resize-image {

  width: 50%;

  -webkit-animation: cd-bounce-in 0.7s;

  -moz-animation: cd-bounce-in 0.7s;

  animation: cd-bounce-in 0.7s;

}

@-webkit-keyframes cd-bounce-in {

  0% {

    width: 0;

  }

  60% {

    width: 55%;

  }

  100% {

    width: 50%;

  }

}

@-moz-keyframes cd-bounce-in {

  0% {

    width: 0;

  }

  60% {

    width: 55%;

  }

  100% {

    width: 50%;

  }

}

@keyframes cd-bounce-in {

  0% {

    width: 0;

  }

  60% {

    width: 55%;

  }

  100% {

    width: 50%;

  }

}

.handle {

  position: absolute;

  height: 44px;

  width: 44px;

  left: 50%;

  top: 50%;

  margin-left: -22px;

  margin-top: -22px;

  border-radius: 50%;

  background: #dc717d url(“https://webdevtrick.com/wp-content/uploads/arrows.svg”) no-repeat center center;

  cursor: move;

  box-shadow: 0 0 0 6px rgba(0, 0, 0, 0.2), 0 0 10px rgba(0, 0, 0, 0.6), inset 0 1px 0 rgba(255, 255, 255, 0.3);

  opacity: 0;

  -webkit-transform: translate3d(0, 0, 0) scale(0);

  -moz-transform: translate3d(0, 0, 0) scale(0);

  -ms-transform: translate3d(0, 0, 0) scale(0);

  -o-transform: translate3d(0, 0, 0) scale(0);

  transform: translate3d(0, 0, 0) scale(0);

}

.handle.draggable {

  background-color: #445b7c;

}

.is-visible .handle {

  opacity: 1;

  -webkit-transform: translate3d(0, 0, 0) scale(1);

  -moz-transform: translate3d(0, 0, 0) scale(1);

  -ms-transform: translate3d(0, 0, 0) scale(1);

  -o-transform: translate3d(0, 0, 0) scale(1);

  transform: translate3d(0, 0, 0) scale(1);

  -webkit-transition: -webkit-transform 0.3s 0.7s, opacity 0s 0.7s;

  -moz-transition: -moz-transform 0.3s 0.7s, opacity 0s 0.7s;

  transition: transform 0.3s 0.7s, opacity 0s 0.7s;

}

function.js

The final step, create a JavaScript file named ‘function.js‘ and put the codes.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

// Code By Webdevtrick ( https://webdevtrick.com )

jQuery(document).ready(function($){

    //check if the .image-container is in the viewport

    //if yes, animate it

    checkPosition($(‘.image-container’));

    $(window).on(‘scroll’, function(){

        checkPosition($(‘.image-container’));

    });

    //make the .handle element draggable and modify .resize-image width according to its position

    drags($(‘.handle’), $(‘.resize-image’), $(‘.image-container’), $(‘.image-label[data-type=”original”]’), $(‘.image-label[data-type=”modified”]’));

    //upadate images label visibility

    $(window).on(‘resize’, function(){

        updateLabel($(‘.image-label[data-type=”modified”]’), $(‘.resize-image’), ‘left’);

        updateLabel($(‘.image-label[data-type=”original”]’), $(‘.resize-image’), ‘right’);

    });

});

function checkPosition(container) {

    if( $(window).scrollTop() + $(window).height()*0.5 > container.offset().top) {

        container.addClass(‘is-visible’);

        //you can uncomment the following line if you don’t have other events to bind to the window scroll

        // $(window).off(‘scroll’);

    }

}

//draggable funtionality

function drags(dragElement, resizeElement, container, labelContainer, labelResizeElement) {

    dragElement.on(“mousedown vmousedown”, function(e) {

        dragElement.addClass(‘draggable’);

        resizeElement.addClass(‘resizable’);

        var dragWidth = dragElement.outerWidth(),

            xPosition = dragElement.offset().left + dragWidth – e.pageX,

            containerOffset = container.offset().left,

            containerWidth = container.outerWidth(),

            minLeft = containerOffset + 10,

            maxLeft = containerOffset + containerWidth – dragWidth – 10;

        dragElement.parents().on(“mousemove vmousemove”, function(e) {

            leftValue = e.pageX + xPosition – dragWidth;

            //constrain the draggable element to move inside his container

            if(leftValue < minLeft ) {

                leftValue = minLeft;

            } else if ( leftValue > maxLeft) {

                leftValue = maxLeft;

            }

            widthValue = (leftValue + dragWidth/2 – containerOffset)*100/containerWidth+‘%’;

            $(‘.draggable’).css(‘left’, widthValue).on(“mouseup vmouseup”, function() {

                $(this).removeClass(‘draggable’);

                resizeElement.removeClass(‘resizable’);

            });

            $(‘.resizable’).css(‘width’, widthValue);

            updateLabel(labelResizeElement, resizeElement, ‘left’);

            updateLabel(labelContainer, resizeElement, ‘right’);

        }).on(“mouseup vmouseup”, function(e){

            dragElement.removeClass(‘draggable’);

            resizeElement.removeClass(‘resizable’);

        });

        e.preventDefault();

    }).on(“mouseup vmouseup”, function(e) {

        dragElement.removeClass(‘draggable’);

        resizeElement.removeClass(‘resizable’);

    });

}

function updateLabel(label, resizeElement, position) {

    if(position == ‘left’) {

        ( label.offset().left + label.outerWidth() < resizeElement.offset().left + resizeElement.outerWidth() ) ? label.removeClass(‘is-hidden’) : label.addClass(‘is-hidden’) ;

    } else {

        ( label.offset().left > resizeElement.offset().left + resizeElement.outerWidth() ) ? label.removeClass(‘is-hidden’) : label.addClass(‘is-hidden’) ;

    }

}

That’s It. Now you have successfully created Image Comparison Slide Using jQuery and CSS, Image Compare Slider. If you have any doubt or question comment down below.

Thanks For Visiting, Keep Visiting.

You Might Also Like

Bootstrap Clean Buttons With Hover Effect | Plain Button UI Kit

Bootstrap Datatable With Sort, Pagination, and Search | Sorting Data Table

PHP Age Calculator Program | Calculate Age In PHP

Bootstrap Tooltip Progress Bar Animation | Percentage Values in Tooltip

Generate Random Numbers In PHP | Numbers In Sort By Order

TAGGED: before and after, comparison slider, CSS, HTML, image, JavaScript, jquery, jquery slider, slider, source code
admin مارس 11, 2023
Share this Article
Facebook Twitter Pinterest Whatsapp Whatsapp LinkedIn Tumblr Reddit VKontakte Telegram Email Copy Link Print
Previous Article image-upload-with-preview-using-jquery-and-css-|-custom-file-input Image Upload With Preview Using jQuery and CSS | Custom File Input
Next Article stairway-hover-navigation-using-css-and-jquery-|-stairy-type-hover Stairway Hover Navigation Using CSS and jQuery | Stairy Type Hover
Leave a comment Leave a comment

اترك تعليقاً إلغاء الرد

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *

Stay Connected

235.3k Followers Like
69.1k Followers Follow
11.6k Followers Pin
56.4k Followers Follow
136k Subscribers Subscribe
4.4k Followers Follow
- Advertisement -
Ad imageAd image

Latest News

bootstrap-clean-buttons-with-hover-effect-|-plain-button-ui-kit
Bootstrap Clean Buttons With Hover Effect | Plain Button UI Kit
Bootstrap مارس 11, 2023
bootstrap-datatable-with-sort,-pagination,-and-search-|-sorting-data-table
Bootstrap Datatable With Sort, Pagination, and Search | Sorting Data Table
Bootstrap مارس 11, 2023
upload-image-in-php-with-mysql-database-|-web-dev-trick
Upload Image In PHP With MySQL Database | Web Dev Trick
PHP مارس 11, 2023
bootstrap-tooltip-progress-bar-animation-|-percentage-values-in-tooltip
Bootstrap Tooltip Progress Bar Animation | Percentage Values in Tooltip
Bootstrap مارس 11, 2023
//

We influence 20 million users and is the number one business and technology news network on the planet

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

[mc4wp_form id=”847″]

LahbabiGuideLahbabiGuide
Follow US

© 2022 Foxiz News Network. Ruby Design Company. All Rights Reserved.

Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc..

[mc4wp_form]
Zero spam, Unsubscribe at any time.

Removed from reading list

Undo
Welcome Back!

Sign in to your account

Lost your password?