Friday, 25 August 2017

Smooth Scroll to div Sticky header

Smooth Scroll to div

    var hash = "#block-block-33";
    jQuery('html, body').animate({
        scrollTop: jQuery(hash).offset().top-300
       }, 800, function(){

         // Add hash (#) to URL when done scrolling (default click behavior)
         //window.location.hash = hash;
         var a=jQuery(hash).offset().top-190;


         //jQuery('.search-icon').text(a);


        window.scrollTo(0,a);
    });

Thursday, 10 August 2017

Post image without form using ajax



<!DOCTYPE html>
<html>
   <head>
      <script src="jquery.min.js"></script>
   </head>
   <body>
      <form method="post" action="upload.php" enctype="multipart/form-data">
         <input type="file" name="fileToUpload" id="fileToUpload" >
         <p class="upload-it">Upload this</p>
         <input type="submit" name="upload" value="Go" >
      </form>
      <?php
         $target_dir = "uploads/";
         $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
         $uploadOk = 1;
         $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
         // Check if image file is a actual image or fake image
         if(isset($_POST["upload"])) {
             $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
             if($check !== false) {
                 if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                     echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
                 } else {
                     echo "Sorry, there was an error uploading your file.";
                 }
                 echo "File is an image - " . $check["mime"] . ".";
                 $uploadOk = 1;
             } else {
                 echo "File is not an image.";
                 $uploadOk = 0;
             }
         }
         ?>
      <script>
         $(function(){
             $(document).on("click", ".upload-it", function() {
                 var file_data = $("#fileToUpload").prop("files")[0];  
                 var form_data = new FormData();                 
                 form_data.append("fileToUpload", file_data);    
                 form_data.append("upload", "upload");           
                 $.ajax({
                             url: "upload.php",
                             dataType: 'script',
                             cache: false,
                             contentType: false,
                             processData: false,
                             data: form_data,                    
                             type: 'post'
                    })
             })
         })
        
      </script>
   </body>
</html>


Wednesday, 26 July 2017

How to print using jquery


Simply just add print.js Library and configure as

JQuery:

    jQuery(".print-button a").on('click', function() {
                  
        jQuery(this).parent().find('.maincontainertoprint').print({

            globalStyles : true,
            mediaPrint : true,
            stylesheet : 'yourstyle.css',
            iframe : false,
            noPrintSelector : ".button, .printbtn",
            deferred: jQuery.Deferred().done(function() { console.log('Printing done', arguments); })
        });
    });


Html:


<div class="maincontainer">
    <div class="print-button">
        <a href="javascript:void(0)" class="">Print</a>
    </div>
    <div class="maincontainertoprint">
        <table>
            <tr>
                <th>S.No.</th>
                <th>Name</th>
                <th>Email</th>
            </tr>
            <tr>
                <td>1</td>
                <td>Bharat</td>
                <td>bharat@gmail.com</td>
            </tr>
            <tr>
                <td>2</td>
                <td>Ravi</td>
                <td>ravi@gmail.com</td>
            </tr>
            <tr>
                <td>3</td>
                <td>Ajay</td>
                <td>ajay@gmail.com</td>
            </tr>
            <tr>
                <td>4</td>
                <td>Gagan</td>
                <td>gagam@gmail.com</td>
            </tr>
        </table>
    </div>
</div>

Library

Monday, 17 July 2017

How to hide date picker on button click

How to hide date picker on button click

#ui-datepicker-div is parent div in which calendar is shown
<input type="button" id="edit-field-date-value-value-datepicker-popup-1" value="close" onclick="return dtPickerClose()" />

function dtPickerClose(){
    jQuery('#ui-datepicker-div').hide();
    jQuery("#edit-field-date-value-value-datepicker-popup-1").attr("class","");
}

this also work in drupal

Friday, 16 June 2017

Add Active position or class on menu on scroll


add Active position or class on scroll

Html :

<div class="title-list-nav">
    <ul>
        <li><a href="#test"></a></li>
        <li><a href="#test1"></a></li>
        <li><a href="#test2"></a></li>
        <li><a href="#test3"></a></li>
    </ul>
</div>

<div id="test">Hi<br><br><br><br><br><br><br></div>
<div id="test1">Hello<br><br><br><br><br><br><br></div>
<div id="test2">You<br><br><br><br><br><br><br></div>
<div id="test3">There?<br><br><br><br><br><br><br></div>


JQuery :

jQuery(window).scroll(function() {
     var cur_pos = jQuery(this).scrollTop();
    jQuery(".title-list-nav ul li").each(function(){

        var ele_top = jQuery(jQuery(this).find("a").attr("href")).offset().top-jQuery("#header").height();

        var ele_bot = jQuery(jQuery(this).find("a").attr("href")).offset().top + jQuery(jQuery(this).find("a").attr("href")).outerHeight(true)-jQuery("#header").height();
       
        if(cur_pos>=ele_top && cur_pos<=ele_bot){
            jQuery(this).addClass("active");
        }else{
            jQuery(this).removeClass("active");
        }
    });
});


Wednesday, 31 May 2017

jQuery Post With and Without Json Response


1.    jQuery Post Method Without json Response


var post_data = 'operation=add&name=Rahul&email=rahul@gmail.com';
jQuery.post('/admin/ajax/do.php', post_data, function(response){
    if(response.msg=="success"){
        alert("Insert Successfully");
    }else{
        alert("Error Occured");
    }
});



2.    jQuery Post Method With json Response


var post_data = 'operation=add&name=Rahul&email=rahul@gmail.com';
jQuery.post('/admin/ajax/do.php', post_data, function(response){
    if(response.msg=="success"){
        alert("Insert Successfully");
    }else{
        alert("Error Occured");
    }
},'json');