Link to home
Start Free TrialLog in
Avatar of Panos
PanosFlag for Germany

asked on

How to scroll to middle of page (with jquery)

Hello experts
I 'm using a function to add a new div onclick loading the content with an ajax call.
The problem is that i cant scroll the new div to the middle of the page.
Below my code.
Any help to fix this?
// JavaScript Document
 $('#adddiv').live('click', function () {
   $.ajax({
     type: "POST",
     url: "thisfunction.cfc?returnformat=json",
     data: {
       method: 'fgetinstruction'
     },
     success: function (f) {
       var formattedResponse = $.parseJSON(f);
       if (jQuery.isArray(formattedResponse) === true) {
         var success = formattedResponse[0]['success'];
         var content = formattedResponse[0]['content'];
         var newid = formattedResponse[0]['newid'];
         if (success === true) {
           $(content).appendTo(instruc);
           $('#' + newid).scrolltocenter();
           return false;
         }
       }
     }
   }, "json")
 });
 jQuery.fn.scrolltocenter = function () {
   var scrollto = this.offset().top + (this.height() / 2);
   this.animate({
     scrollTop: scrollto
   });
   return this;
 }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Panos

ASKER

Hi  julianH
Thank you for your correction and the great help.
It is working fine
Avatar of Panos

ASKER

Thank you
regards
panos
You are welcome - thanks for the points.