jQuery Resizable Widget

jQuery Resizable Widget allows you to change the size of an element.

Consider the following HTML and CSS

<div id="redDiv" class="divClass" style="background-color: red">
    Red Div
</div>

<style>
    .divClass {
        font-family: Arial;
        height: 150px;
        width: 150px;
        text-align: center;
        color: white;
    }
</style>

To make the div element resizable
$(document).ready(function () {
    $('#redDiv').resizable();
});

Resizing redDiv will also resize blueDiv

HTML
<div id="redDiv" class="divClass" style="background-color: red">
    Red Div
</div>
<br />
<div id="blueDiv" class="divClass" style="background-color: blue">
    Red Div
</div>

jQuery
$(document).ready(function () {
    $('#redDiv').resizable({
        alsoResize: '#blueDiv'
    });
});

Constrains re-sizing to within the bounds of the container div

HTML
<div id="container" style="border: 3px solid black;
                            height: 300px; width: 300px; padding: 5px">
    <div id="redDiv" class="divClass" style="background-color: red">
        Red Div
    </div>
</div>

jQuery
$('#redDiv').resizable({
    containment: '#container'
});

The following example handles start, stop and resize events of resizable widget

<%@ Page Language="C#" AutoEventWireup="true"
    CodeBehind="WebForm1.aspx.cs" Inherits="Demo.WebForm1" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery-1.11.2.js"></script>
    <script src="jquery-ui.js"></script>
    <link href="jquery-ui.css" rel="stylesheet" />
    <script type="text/javascript">
        $(document).ready(function () {
            $('#redDiv').resizable({
                start: function (event, ui) {
                    $('#startDimensions').html(getDimensions(event, ui));
                },
                stop: function (event, ui) {
                    $('#stopDimensions').html(getDimensions(event, ui));
                },
                resize: function (event, ui) {
                    $('#resizingDimensions').html(getDimensions(event, ui));
                }
            });

            function getDimensions(event, ui) {
                var html = 'Height = ' + ui.size.height + '<br/>';
                html += 'Width = ' + ui.size.width;
                return html;
            }
        });
    </script>
    <style>
        .divClass {
            font-family: Arial;
            height: 150px;
            width: 150px;
            text-align: center;
            color: white;
        }
    </style>
</head>
<body style="font-family: Arial">
    <form id="form1" runat="server">
        <table border="1" style="border-collapse: collapse;">
            <tr>
                <td>Dimensions at Start</td>
                <td>
                    <div id="startDimensions"></div>
                </td>
            </tr>
            <tr>
                <td>Dimensions while Resizing</td>
                <td>
                    <div id="resizingDimensions"></div>
                </td>
            </tr>
            <tr>
                <td>Dimensions at Stop</td>
                <td>
                    <div id="stopDimensions"></div>
                </td>
            </tr>
        </table>
        <br />
        <div id="redDiv" class="divClass" style="background-color: red">
            Red Div
        </div>
    </form>
</body>
</html>

Comments

Popular posts from this blog

Automatically send Birthday email using C#.Net

Drag and Drop multiple File upload using jQuery AJAX in ASP.Net using C# and VB.Net

Difference between each and map in jquery