Solved

How to scroll to middle of page (with jquery)

Posted on 2014-04-29
4
2,418 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 54

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 54

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SharePoint 2013 Searchbox Branding 11 78
Angular 2 complaining about map file 7 86
Wordpress Taxonomy 2 28
jQuery slideToggle working incorrectly 3 24
I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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…

777 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