Get element closest to the middle of the screen
Trying to get the element that is closest to the middle of the screen. Similar question has already been posted here. This works great unless you have content in a container that is larger than the view port. In my scenario by default the content does not exceed the height of the viewport, but there is functionality to allow the user to add more input fields which can mean it goes beyond the viewport.
When you are viewing an element, it's supposed to stay in view (full opacity) until another element is more in view, at which point, the original will go to low opacity.
See my fiddle here: https://jsfiddle.net/6rzuqknx/4/ - once you get half way through first element, it goes to low opacity. The two functions below are the functions I currently use to get the closest element:
// calculate what element is in middle of viewport
function closest(array, number) {
var num = 0;
for (var i = array.length - 1; i >= 0; i--) {
if (Math.abs(number - array[i].position) < Math.abs(number - array[num].position)) {
num = i;
}
}
return array[num].element;
}
// get positions of all question divs
function getPositions() {
var positions = ;
$('.question').each(function() {
// $(this).removeClass("showing");
positions.push({
position: $(this).position().top,
element: $(this)
});
});
return positions;
}
Apologies for the mess of my code, it's within a fairly big project and it wasn't the easiest snipping part of it.
Cheers guys
javascript jquery
add a comment |
Trying to get the element that is closest to the middle of the screen. Similar question has already been posted here. This works great unless you have content in a container that is larger than the view port. In my scenario by default the content does not exceed the height of the viewport, but there is functionality to allow the user to add more input fields which can mean it goes beyond the viewport.
When you are viewing an element, it's supposed to stay in view (full opacity) until another element is more in view, at which point, the original will go to low opacity.
See my fiddle here: https://jsfiddle.net/6rzuqknx/4/ - once you get half way through first element, it goes to low opacity. The two functions below are the functions I currently use to get the closest element:
// calculate what element is in middle of viewport
function closest(array, number) {
var num = 0;
for (var i = array.length - 1; i >= 0; i--) {
if (Math.abs(number - array[i].position) < Math.abs(number - array[num].position)) {
num = i;
}
}
return array[num].element;
}
// get positions of all question divs
function getPositions() {
var positions = ;
$('.question').each(function() {
// $(this).removeClass("showing");
positions.push({
position: $(this).position().top,
element: $(this)
});
});
return positions;
}
Apologies for the mess of my code, it's within a fairly big project and it wasn't the easiest snipping part of it.
Cheers guys
javascript jquery
1
"See my fiddle here: jsfiddle.net/6rzuqknx/4" Please put your runnable examples here, on-site using Stack Snippets (the[<>]toolbar button; here's how to do one). Always be sure to reduce the example to a Minimal, Complete, and Verifiable example.
– T.J. Crowder
Nov 23 '18 at 10:44
2
Woulddocument.elementFromPoint(document.offsetWidth / 2, document.offsetHeight / 2)work?
– alex
Nov 23 '18 at 10:45
1
@alex Thanks Alex! That worked - if you want to write an answer I'll mark as answered!
– Tommy Jinks
Nov 23 '18 at 11:34
add a comment |
Trying to get the element that is closest to the middle of the screen. Similar question has already been posted here. This works great unless you have content in a container that is larger than the view port. In my scenario by default the content does not exceed the height of the viewport, but there is functionality to allow the user to add more input fields which can mean it goes beyond the viewport.
When you are viewing an element, it's supposed to stay in view (full opacity) until another element is more in view, at which point, the original will go to low opacity.
See my fiddle here: https://jsfiddle.net/6rzuqknx/4/ - once you get half way through first element, it goes to low opacity. The two functions below are the functions I currently use to get the closest element:
// calculate what element is in middle of viewport
function closest(array, number) {
var num = 0;
for (var i = array.length - 1; i >= 0; i--) {
if (Math.abs(number - array[i].position) < Math.abs(number - array[num].position)) {
num = i;
}
}
return array[num].element;
}
// get positions of all question divs
function getPositions() {
var positions = ;
$('.question').each(function() {
// $(this).removeClass("showing");
positions.push({
position: $(this).position().top,
element: $(this)
});
});
return positions;
}
Apologies for the mess of my code, it's within a fairly big project and it wasn't the easiest snipping part of it.
Cheers guys
javascript jquery
Trying to get the element that is closest to the middle of the screen. Similar question has already been posted here. This works great unless you have content in a container that is larger than the view port. In my scenario by default the content does not exceed the height of the viewport, but there is functionality to allow the user to add more input fields which can mean it goes beyond the viewport.
When you are viewing an element, it's supposed to stay in view (full opacity) until another element is more in view, at which point, the original will go to low opacity.
See my fiddle here: https://jsfiddle.net/6rzuqknx/4/ - once you get half way through first element, it goes to low opacity. The two functions below are the functions I currently use to get the closest element:
// calculate what element is in middle of viewport
function closest(array, number) {
var num = 0;
for (var i = array.length - 1; i >= 0; i--) {
if (Math.abs(number - array[i].position) < Math.abs(number - array[num].position)) {
num = i;
}
}
return array[num].element;
}
// get positions of all question divs
function getPositions() {
var positions = ;
$('.question').each(function() {
// $(this).removeClass("showing");
positions.push({
position: $(this).position().top,
element: $(this)
});
});
return positions;
}
Apologies for the mess of my code, it's within a fairly big project and it wasn't the easiest snipping part of it.
Cheers guys
javascript jquery
javascript jquery
asked Nov 23 '18 at 10:40
Tommy JinksTommy Jinks
662519
662519
1
"See my fiddle here: jsfiddle.net/6rzuqknx/4" Please put your runnable examples here, on-site using Stack Snippets (the[<>]toolbar button; here's how to do one). Always be sure to reduce the example to a Minimal, Complete, and Verifiable example.
– T.J. Crowder
Nov 23 '18 at 10:44
2
Woulddocument.elementFromPoint(document.offsetWidth / 2, document.offsetHeight / 2)work?
– alex
Nov 23 '18 at 10:45
1
@alex Thanks Alex! That worked - if you want to write an answer I'll mark as answered!
– Tommy Jinks
Nov 23 '18 at 11:34
add a comment |
1
"See my fiddle here: jsfiddle.net/6rzuqknx/4" Please put your runnable examples here, on-site using Stack Snippets (the[<>]toolbar button; here's how to do one). Always be sure to reduce the example to a Minimal, Complete, and Verifiable example.
– T.J. Crowder
Nov 23 '18 at 10:44
2
Woulddocument.elementFromPoint(document.offsetWidth / 2, document.offsetHeight / 2)work?
– alex
Nov 23 '18 at 10:45
1
@alex Thanks Alex! That worked - if you want to write an answer I'll mark as answered!
– Tommy Jinks
Nov 23 '18 at 11:34
1
1
"See my fiddle here: jsfiddle.net/6rzuqknx/4" Please put your runnable examples here, on-site using Stack Snippets (the
[<>] toolbar button; here's how to do one). Always be sure to reduce the example to a Minimal, Complete, and Verifiable example.– T.J. Crowder
Nov 23 '18 at 10:44
"See my fiddle here: jsfiddle.net/6rzuqknx/4" Please put your runnable examples here, on-site using Stack Snippets (the
[<>] toolbar button; here's how to do one). Always be sure to reduce the example to a Minimal, Complete, and Verifiable example.– T.J. Crowder
Nov 23 '18 at 10:44
2
2
Would
document.elementFromPoint(document.offsetWidth / 2, document.offsetHeight / 2) work?– alex
Nov 23 '18 at 10:45
Would
document.elementFromPoint(document.offsetWidth / 2, document.offsetHeight / 2) work?– alex
Nov 23 '18 at 10:45
1
1
@alex Thanks Alex! That worked - if you want to write an answer I'll mark as answered!
– Tommy Jinks
Nov 23 '18 at 11:34
@alex Thanks Alex! That worked - if you want to write an answer I'll mark as answered!
– Tommy Jinks
Nov 23 '18 at 11:34
add a comment |
1 Answer
1
active
oldest
votes
To expand from my comment, this should do it:
const centeredElement = document.elementFromPoint(
document.body.offsetWidth / 2, document.body.offsetHeight / 2
);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445105%2fget-element-closest-to-the-middle-of-the-screen%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
To expand from my comment, this should do it:
const centeredElement = document.elementFromPoint(
document.body.offsetWidth / 2, document.body.offsetHeight / 2
);
add a comment |
To expand from my comment, this should do it:
const centeredElement = document.elementFromPoint(
document.body.offsetWidth / 2, document.body.offsetHeight / 2
);
add a comment |
To expand from my comment, this should do it:
const centeredElement = document.elementFromPoint(
document.body.offsetWidth / 2, document.body.offsetHeight / 2
);
To expand from my comment, this should do it:
const centeredElement = document.elementFromPoint(
document.body.offsetWidth / 2, document.body.offsetHeight / 2
);
edited Nov 23 '18 at 11:53
Nedko Dimitrov
428515
428515
answered Nov 23 '18 at 11:35
alexalex
339k167766912
339k167766912
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445105%2fget-element-closest-to-the-middle-of-the-screen%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
"See my fiddle here: jsfiddle.net/6rzuqknx/4" Please put your runnable examples here, on-site using Stack Snippets (the
[<>]toolbar button; here's how to do one). Always be sure to reduce the example to a Minimal, Complete, and Verifiable example.– T.J. Crowder
Nov 23 '18 at 10:44
2
Would
document.elementFromPoint(document.offsetWidth / 2, document.offsetHeight / 2)work?– alex
Nov 23 '18 at 10:45
1
@alex Thanks Alex! That worked - if you want to write an answer I'll mark as answered!
– Tommy Jinks
Nov 23 '18 at 11:34