Modern web development focuses more and more on user interaction due to which there are tons of cool interactive plugins available to be integrated in any web platform. Twenty Twenty is one such plugin which provides interactive image comparison visualization. This JQuery based plugin can be utilized to showcase product comparison in a more interactive way.
So, today, I shall be demonstrating Twenty Twenty JQuery based plugin with ASP.NET MVC5 platform.
Following are some prerequisites before you proceed further in this tutorial,
- Download Twenty Twenty JQuery plugin.
- Knowledge of ASP.NET MVC5.
- Knowledge of HTML.
- Knowledge of JavaScript.
- Knowledge of Bootstrap.
- Knowledge of Jquery.
- Knowledge of C# Programming.
You can download the complete source code for this tutorial or you can follow the step by step discussion below. The sample code is being developed in Microsoft Visual Studio 2015 Enterprise.
Let's begin now.
Step 1
Create a new MVC project and name it "ImgDiff".
Step 2
Make sure to download "Twenty Twenty" JQuery plugin, then create new folder "Plugin" under project root and copy "CSS" & "JS" folders of the plugin into this new folder.
Step 3
Open "Views->Shared->_Layout.cshtml"file and replace following code in it
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>@ViewBag.Title</title>
- @Styles.Render("~/Content/css")
- @Scripts.Render("~/bundles/modernizr")
-
-
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" />
-
-
- @Styles.Render("~/Plugin/Twenty-Twenty/css")
- </head>
- <body>
- <div class="navbar navbar-inverse navbar-fixed-top">
- <div class="container">
- <div class="navbar-header">
- <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- </button>
- </div>
- </div>
- </div>
- <div class="container body-content">
- @RenderBody()
- <hr />
- <footer>
- <center>
- <p><strong>Copyright © @DateTime.Now.Year - <a href="http://wwww.asmak9.com/">Asma's Blog</a>.</strong> All rights reserved.</p>
- </center>
- </footer>
- </div>
-
- @*Scripts*@
- @Scripts.Render("~/bundles/jquery")
- @Scripts.Render("~/bundles/bootstrap")
-
-
- @Scripts.Render("~/Plugin/Twenty-Twenty")
-
- @RenderSection("scripts", required: false)
- </body>
- </html>
In the above code, we have simply set up our basic layout structure and we have also included required CCS & Java Script files references.
Step 4
Now, create new controller under "Controllers" folder and name it "ImgDiffController.cs".
Step 5
Open "Controllers->ImgDiffController.cs" file and replace following code in it
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace ImgDiff.Controllers
- {
- public class ImgDiffController : Controller
- {
- #region Index view method.
-
- #region Get: /ImgDiff/Index.
-
-
-
-
-
- public ActionResult Index()
- {
- try
- {
- }
- catch (Exception ex)
- {
-
- Console.Write(ex);
- }
-
-
- return this.View();
- }
-
- #endregion
-
- #endregion
- }
- }
The above piece of code simply returns a basic view without any specific processing.
Step 6
Create "Index.cshtml" file under "Views->ImgDiff" folder and replace following code in it
- @{
- ViewBag.Title = "ASP.NET MVC5: Jquery Image Diff";
- }
-
-
- <div class="row">
- <div class="panel-heading">
- <div class="col-md-8">
- <h3>
- <i class="fa fa-file-text-o"></i>
- <span>ASP.NET MVC5: Jquery Image Diff</span>
- </h3>
- </div>
- </div>
- </div>
-
- <div class="row">
- <section class="col-md-7 col-md-push-3">
- <div class="large-8 columns">
- <div class="twentytwenty-container">
- <img src="~/Content/img/ori.jpg" />
- <img src="~/Content/img/bright.jpg" />
- </div>
- </div>
- </section>
- </div>
In the above piece of code, I have placed my two images, whose differences I want to showcase within the "
Twenty Twenty" JQuery plugin container i.e.
- <div class="twentytwenty-container">
- <img src="~/Content/img/ori.jpg" />
- <img src="~/Content/img/bright.jpg" />
- </div>
Step 7
Create an "custom-img-diff.js" file within "Scripts" folder and replace the following code in it
- $(document).ready(function ()
- {
- $(".twentytwenty-container").twentytwenty({ default_offset_pct: 0.5 });
- });
In the above, I have called the "
Twenty Twenty" JQuery plugin to showcase my images difference/comparison. I have also set the default compare slider position to be centered, it can be set between the range of 0 to 1.
Step 8
Let's execute the project, you will be able to see the following
Conclusion
In this article, you learned about the interactive visual image comparison JQuery based plugin Twenty Twenty. You also learned about the integration of the Twenty Twenty JQuery plugin with ASP.NET MVC5 platform along with the utilization of Twenty Twenty JQuery plugin.