Solved

How to scroll to middle of page (with jquery)

Posted on 2014-04-29
4
2,312 Views
Last Modified: 2014-04-29
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

0
Comment
Question by:Panos
  • 2
  • 2
4 Comments
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 40029220
Firstup consider changing .live() to .on() - the former is being deprecated.

Secondly - set your dataType : 'json' - that way you won't have to use this line

var formattedResponse = $.parseJSON(f);

As you have it at the moment is the method used for .post() i.e. to add the 'json' parameter after the definition of the callback.

For .ajax use
type: "POST",
     url: "thisfunction.cfc?returnformat=json",
     data: {
       method: 'fgetinstruction'
     },
     dataType: 'json',
     success: 
...
}

Open in new window

Then to your actual problem
1. You need to subtract from offset top not add
2. The value you need to subtract is half the difference between the window height and the height of your div
3. Finally - you need to scroll the body not the div
Here is some code that should do what you want
 jQuery.fn.scrolltocenter = function () {
   var scrollto = this.offset().top - (($(window).height() - this.height())/ 2);
   $('html,body').animate({
     scrollTop: scrollto
   });
   return this;
 }

Open in new window

0
 
LVL 2

Author Comment

by:Panos
ID: 40029234
Hi  julianH
Thank you for your correction and the great help.
It is working fine
0
 
LVL 2

Author Closing Comment

by:Panos
ID: 40029235
Thank you
regards
panos
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 40029398
You are welcome - thanks for the points.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now