Sunday, 19 July 2015

jQuery Effects

1. jQuery hide() ans show()


Syntax-

$("#hide").click(function(){
    $("p").hide();
});

$("#show").click(function(){
    $("p").show();
});

Hide() function with speed parameter:

$("button").click(function(){
    $("p").hide(1000);
});

2. jQuery toggle()

you can toggle between the hide() and show() methods with the toggle() method.

$("button").click(function(){
    $("p").toggle();
});

3. jQuery Fade Method

With jQuery you can fade an element in and out of visibility.
jQuery has the following fade methods:
  • fadeIn()
  • fadeOut()
  • fadeToggle()
  • fadeTo()
$("button").click(function(){
    $("#div1").fadeIn();
    $("#div2").fadeIn("slow");
    $("#div3").fadeIn(3000);
});

fadeOut() and fadeToggle() are same as above.

->fadeTo()-

$(selector).fadeTo(speed,opacity,callback);

The required speed parameter specifies the duration of the effect. It can take the following values: "slow", "fast", or milliseconds.
The required opacity parameter in the fadeTo() method specifies fading to a given opacity (value between 0 and 1).
The optional callback parameter is a function to be executed after the function completes.
$("button").click(function(){
    $("#div1").fadeTo("slow", 0.15);
    $("#div2").fadeTo("slow", 0.4);
    $("#div3").fadeTo("slow", 0.7);
});

4. jQuery slide Method



<div id="flip">Click to slide the panel down or up</div>
<div id="panel">Hello world!</div>

jQuery has the following slide methods:
a ) slideDown()
   $("#flip").click(function(){
     $("#panel").slideDown();  });
b) slideUp()
$("#flip").click(function(){
    $("#panel").slideUp();
});
c) slideToggle()
$("#flip").click(function(){
    $("#panel").slideToggle();
});


Wednesday, 20 May 2015

jQuery Syntax

The jQuery syntax is tailor made for selecting HTML elements and performing some action on the element(s).
Basic syntax is: $(selector).action()
Example:
(this).hide() - hides the current element.
$("p").hide() - hides all <p> elements.
$(".test").hide() - hides all elements with class="test".
$("#test").hide() - hides the element with id="test".
The Element Selector:
For Button->
When a user clicks on a button, all <p> elements will be hidden:
$(document).ready(function(){
    $("button").click(function(){
        $("p").hide();
    });
});
The #id Selector:
When a user clicks on a button, the element with id="test" will be hidden:
$(document).ready(function(){
    $("button").click(function(){
        $("#test").hide();
    });
});

The .class Selector:
When a user clicks on a button, the elements with class="test" will be hidden:
$(document).ready(function(){
    $("button").click(function(){
        $(".test").hide();
    });
});

Same above functions  can be in separate file -
If your website contains a lot of pages, and you want your jQuery functions to be easy to maintain, you can put your jQuery functions in a separate .js file

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js">
</script>
<script src="my_jquery_functions.js"></script>
</head


jQuery Introduction

jQuery is a fast, small, and feature-rich JavaScript library with a nice motto − Write less, do more.

jQuery simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development.


jQuery is a JavaScript toolkit designed to simplify various tasks by writing less code. Here is the list of important core features supported by jQuery −
1)DOM manipulation − The jQuery made it easy to select DOM elements, traverse them and modifying their content by using cross-browser open source selector engine called Sizzle.
2)Event handling − The jQuery offers an elegant way to capture a wide variety of events, such as a user clicking on a link, without the need to clutter the HTML code itself with event handlers.
3)AJAX Support − The jQuery helps you a lot to develop a responsive and feature-rich site using AJAX technology.
4)Animations − The jQuery comes with plenty of built-in animation effects which you can use in your websites.
5)Lightweight − The jQuery is very lightweight library - about 19KB in size ( Minified and gzipped )
6)Latest Technology − The jQuery supports CSS3 selectors and basic XPath syntax.

How to use jQuery?

There are two ways to use jQuery.
a)Local Installation − You can download jQuery library on your local machine and include it in your HTML code.
b)CDN Based Version − You can include jQuery library into your HTML code directly from Content Delivery Network (CDN).

Local Installation

  • Go to the https://jquery.com/download/ to download the latest version available.
  • Now put downloaded jquery-2.1.3.min.js file in a directory of your website, e.g. /jquery.

<head>
<script type = "text/javascript" src ="/jquery/jquery-2.1.3.min.js"></script> 
<script>
$(document).ready(function(){
   //code goes here 
});
</script>
</head>

CDN Based Version

You can include jQuery library into your HTML code directly from Content Delivery Network (CDN). Google and Microsoft provides content deliver for the latest version.
We are using Google CDN version of the library throughout this
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
//your code goes here
});
</script>
</head> 

Calling a jQuery library functions


f you want an event to work on your page, you should call it inside the $(document).ready() function. Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded.
To do this, we register a ready event for the document as follows −
$(document).ready(function(){
   //your code goes here
});

Tuesday, 15 July 2014

Pure Virtual Functions and Abstract Classes

Sometimes implementation of all function cannot be provided in a base class because we don’t know the implementation. Such a class is called abstract classFor example, let Shape be a base class. We cannot provide implementation of function draw() in Shape, but we know every derived class must have implementation of draw(). Similarly an Animal class doesn’t have implementation of move() (assuming that all animals move), but all animals must know how to move. We cannot create objects of abstract classes.

A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have implementation, we only declare it. A pure virtual function is declared by assigning 0 in declaration

#include<iostream>
using namespace std;
 
class Base
{
   int x;
public:
    virtual void fun() = 0;
    int getX() { return x; }
};
 

class Derived: public Base
{
    int y;
public:
     void fun() { cout << "fun() called"; }
};
 
int main(void)
{
    Derived d;
    d.fun();
    return 0;
}
Output:
fun() called


Some Interesting Facts:
1) A class is abstract if it has at least one pure virtual function.
In the following example, Test is an abstract class because it has a pure virtual function show().
#include<iostream>
using namespace std;
 
class Test
{
   int x;
public:
    virtual void show() = 0;
    int getX() { return x; }
};
 
int main(void)
{
    Test t;
    return 0;
}
Output:
Compiler Error: cannot declare variable 't' to be of abstract
 type 'Test' because the following virtual functions are pure 
within 'Test': note:  virtual void Test::show() 

2) We can have pointers and references of abstract class type. For example the following program works fine.
#include<iostream>
using namespace std;
 
class Base
{
public:
    virtual void show() = 0;
};
 
class Derived: public Base
{
public:
    void show() { cout << "In Derived \n"; }
};
 
int main(void)
{
    Base *bp = new Derived();
    bp->show();
    return 0;
}
Output:
In Derived 

3) If we do not override the pure virtual function in derived class, then derived class also becomes abstract class. The following example demonstrates the same.
#include<iostream>
using namespace std;
class Base
{
public:
    virtual void show() = 0;
};
 
class Derived : public Base { };
 
int main(void)
{
  Derived d;
  return 0;
}
Compiler Error: cannot declare variable 'd' to be of abstract type 
'Derived'  because the following virtual functions are pure within
'Derived': virtual void Base::show()