Margin condition in javascript
I am trying to build a slideshow that pretty much works by changing the margins in my CSS.
So far it's working well but I want the next and previous button to disappear when the margins have a certain value and I couldn't find a solution.
Here is my js code:
const nextBtn = document.querySelector('#slideNext');
const prevBtn = document.querySelector('#slidePrev');
const slider = document.querySelector('.boxes-container');
const slideWidth = 250;
// Slider initial margin
slider.style.marginLeft = "0";
nextBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) - slideWidth) + 'px';
}
prevBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) + slideWidth) + 'px';
}
if (slider.style.marginLeft == "0") {
prevBtn.style.display = "none";
} else if (slider.style.marginLeft == "-1000px") {
nextBtn.style.display = "none";
} else {
nextBtn.style.display = "inline-block";
prevBtn.style.display = "inline-block";
}
My issue is on the last part: I don't know how to deal with an if/else statement that would modify the display of my buttons when the margin reach a certain value.
It would be awesome if anyone could point me in the right direction as I've spent some time trying to solve this on my own but couldn't find the right solution.
Thanks for your help!!
javascript if-statement conditional
add a comment |
I am trying to build a slideshow that pretty much works by changing the margins in my CSS.
So far it's working well but I want the next and previous button to disappear when the margins have a certain value and I couldn't find a solution.
Here is my js code:
const nextBtn = document.querySelector('#slideNext');
const prevBtn = document.querySelector('#slidePrev');
const slider = document.querySelector('.boxes-container');
const slideWidth = 250;
// Slider initial margin
slider.style.marginLeft = "0";
nextBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) - slideWidth) + 'px';
}
prevBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) + slideWidth) + 'px';
}
if (slider.style.marginLeft == "0") {
prevBtn.style.display = "none";
} else if (slider.style.marginLeft == "-1000px") {
nextBtn.style.display = "none";
} else {
nextBtn.style.display = "inline-block";
prevBtn.style.display = "inline-block";
}
My issue is on the last part: I don't know how to deal with an if/else statement that would modify the display of my buttons when the margin reach a certain value.
It would be awesome if anyone could point me in the right direction as I've spent some time trying to solve this on my own but couldn't find the right solution.
Thanks for your help!!
javascript if-statement conditional
1
What seems to be the problem with the last part? What is not working with the if/else statement?
– Jack Bashford
Nov 12 '18 at 2:24
Sorry I haven't been quite clear about that. The issue is that the buttons doesn't switch state meaning that you can keep on clicking it and adding margin.
– Moromain
Nov 12 '18 at 2:25
1
i think the problem is that your if statements are only run once, but not every time the margin has updated. I would advice you to add this block of code into a function and then run these checks every time either nextBtn or the prevBtn are being clicked (when the margin is being changed).
– Andresson
Nov 12 '18 at 2:28
add a comment |
I am trying to build a slideshow that pretty much works by changing the margins in my CSS.
So far it's working well but I want the next and previous button to disappear when the margins have a certain value and I couldn't find a solution.
Here is my js code:
const nextBtn = document.querySelector('#slideNext');
const prevBtn = document.querySelector('#slidePrev');
const slider = document.querySelector('.boxes-container');
const slideWidth = 250;
// Slider initial margin
slider.style.marginLeft = "0";
nextBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) - slideWidth) + 'px';
}
prevBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) + slideWidth) + 'px';
}
if (slider.style.marginLeft == "0") {
prevBtn.style.display = "none";
} else if (slider.style.marginLeft == "-1000px") {
nextBtn.style.display = "none";
} else {
nextBtn.style.display = "inline-block";
prevBtn.style.display = "inline-block";
}
My issue is on the last part: I don't know how to deal with an if/else statement that would modify the display of my buttons when the margin reach a certain value.
It would be awesome if anyone could point me in the right direction as I've spent some time trying to solve this on my own but couldn't find the right solution.
Thanks for your help!!
javascript if-statement conditional
I am trying to build a slideshow that pretty much works by changing the margins in my CSS.
So far it's working well but I want the next and previous button to disappear when the margins have a certain value and I couldn't find a solution.
Here is my js code:
const nextBtn = document.querySelector('#slideNext');
const prevBtn = document.querySelector('#slidePrev');
const slider = document.querySelector('.boxes-container');
const slideWidth = 250;
// Slider initial margin
slider.style.marginLeft = "0";
nextBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) - slideWidth) + 'px';
}
prevBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) + slideWidth) + 'px';
}
if (slider.style.marginLeft == "0") {
prevBtn.style.display = "none";
} else if (slider.style.marginLeft == "-1000px") {
nextBtn.style.display = "none";
} else {
nextBtn.style.display = "inline-block";
prevBtn.style.display = "inline-block";
}
My issue is on the last part: I don't know how to deal with an if/else statement that would modify the display of my buttons when the margin reach a certain value.
It would be awesome if anyone could point me in the right direction as I've spent some time trying to solve this on my own but couldn't find the right solution.
Thanks for your help!!
javascript if-statement conditional
javascript if-statement conditional
edited Nov 12 '18 at 2:29
asked Nov 12 '18 at 2:18
Moromain
456
456
1
What seems to be the problem with the last part? What is not working with the if/else statement?
– Jack Bashford
Nov 12 '18 at 2:24
Sorry I haven't been quite clear about that. The issue is that the buttons doesn't switch state meaning that you can keep on clicking it and adding margin.
– Moromain
Nov 12 '18 at 2:25
1
i think the problem is that your if statements are only run once, but not every time the margin has updated. I would advice you to add this block of code into a function and then run these checks every time either nextBtn or the prevBtn are being clicked (when the margin is being changed).
– Andresson
Nov 12 '18 at 2:28
add a comment |
1
What seems to be the problem with the last part? What is not working with the if/else statement?
– Jack Bashford
Nov 12 '18 at 2:24
Sorry I haven't been quite clear about that. The issue is that the buttons doesn't switch state meaning that you can keep on clicking it and adding margin.
– Moromain
Nov 12 '18 at 2:25
1
i think the problem is that your if statements are only run once, but not every time the margin has updated. I would advice you to add this block of code into a function and then run these checks every time either nextBtn or the prevBtn are being clicked (when the margin is being changed).
– Andresson
Nov 12 '18 at 2:28
1
1
What seems to be the problem with the last part? What is not working with the if/else statement?
– Jack Bashford
Nov 12 '18 at 2:24
What seems to be the problem with the last part? What is not working with the if/else statement?
– Jack Bashford
Nov 12 '18 at 2:24
Sorry I haven't been quite clear about that. The issue is that the buttons doesn't switch state meaning that you can keep on clicking it and adding margin.
– Moromain
Nov 12 '18 at 2:25
Sorry I haven't been quite clear about that. The issue is that the buttons doesn't switch state meaning that you can keep on clicking it and adding margin.
– Moromain
Nov 12 '18 at 2:25
1
1
i think the problem is that your if statements are only run once, but not every time the margin has updated. I would advice you to add this block of code into a function and then run these checks every time either nextBtn or the prevBtn are being clicked (when the margin is being changed).
– Andresson
Nov 12 '18 at 2:28
i think the problem is that your if statements are only run once, but not every time the margin has updated. I would advice you to add this block of code into a function and then run these checks every time either nextBtn or the prevBtn are being clicked (when the margin is being changed).
– Andresson
Nov 12 '18 at 2:28
add a comment |
2 Answers
2
active
oldest
votes
You created the logic correctly. The problem is that you don't check the margin every time the button is clicked (javascript is not reactive by default).
You have to wrap the margin's logic into a function and then check the margin values every time the button is clicked.
const nextBtn = document.querySelector('#slideNext');
const prevBtn = document.querySelector('#slidePrev');
const slider = document.querySelector('.boxes-container');
const slideWidth = 250;
// Slider initial margin
slider.style.marginLeft = "0";
nextBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) - slideWidth) + 'px';
checkMargin();
}
prevBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) + slideWidth) + 'px';
checkMargin();
}
function checkMargin() {
if (slider.style.marginLeft == "0") {
prevBtn.style.display = "none";
} else if (slider.style.marginLeft == "-1000px") {
nextBtn.style.display = "none";
} else {
nextBtn.style.display = "inline-block";
prevBtn.style.display = "inline-block";
}
}
There are other methods to make the DOM reactive, like MutationObserver. But it may be an overkill to this task.
add a comment |
The reason why it wasn't working was because I wasn't checking for the margins when the buttons were clicked.
I changed my code as follow:
const nextBtn = document.querySelector('#slideNext');
const prevBtn = document.querySelector('#slidePrev');
const slider = document.querySelector('.boxes-container');
const slideWidth = 250;
// Slider initial margin
slider.style.marginLeft = "0px";
function check() {
if (slider.style.marginLeft == "0px") {
prevBtn.style.display = "none";
} else if (slider.style.marginLeft == "-1000px") {
nextBtn.style.display = "none";
} else {
nextBtn.style.display = "inline-block";
prevBtn.style.display = "inline-block";
}
}
window.onload = check(); // Check the margins when the page is loaded
nextBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) - slideWidth) + 'px';
check();
}
prevBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) + slideWidth) + 'px';
check();
}
- Wrapped the margin logic inside of a
check()
function - Check this function on page load and everytime the buttons are clicked
Thanks to Gustavo for his precious help!
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%2f53255234%2fmargin-condition-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You created the logic correctly. The problem is that you don't check the margin every time the button is clicked (javascript is not reactive by default).
You have to wrap the margin's logic into a function and then check the margin values every time the button is clicked.
const nextBtn = document.querySelector('#slideNext');
const prevBtn = document.querySelector('#slidePrev');
const slider = document.querySelector('.boxes-container');
const slideWidth = 250;
// Slider initial margin
slider.style.marginLeft = "0";
nextBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) - slideWidth) + 'px';
checkMargin();
}
prevBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) + slideWidth) + 'px';
checkMargin();
}
function checkMargin() {
if (slider.style.marginLeft == "0") {
prevBtn.style.display = "none";
} else if (slider.style.marginLeft == "-1000px") {
nextBtn.style.display = "none";
} else {
nextBtn.style.display = "inline-block";
prevBtn.style.display = "inline-block";
}
}
There are other methods to make the DOM reactive, like MutationObserver. But it may be an overkill to this task.
add a comment |
You created the logic correctly. The problem is that you don't check the margin every time the button is clicked (javascript is not reactive by default).
You have to wrap the margin's logic into a function and then check the margin values every time the button is clicked.
const nextBtn = document.querySelector('#slideNext');
const prevBtn = document.querySelector('#slidePrev');
const slider = document.querySelector('.boxes-container');
const slideWidth = 250;
// Slider initial margin
slider.style.marginLeft = "0";
nextBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) - slideWidth) + 'px';
checkMargin();
}
prevBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) + slideWidth) + 'px';
checkMargin();
}
function checkMargin() {
if (slider.style.marginLeft == "0") {
prevBtn.style.display = "none";
} else if (slider.style.marginLeft == "-1000px") {
nextBtn.style.display = "none";
} else {
nextBtn.style.display = "inline-block";
prevBtn.style.display = "inline-block";
}
}
There are other methods to make the DOM reactive, like MutationObserver. But it may be an overkill to this task.
add a comment |
You created the logic correctly. The problem is that you don't check the margin every time the button is clicked (javascript is not reactive by default).
You have to wrap the margin's logic into a function and then check the margin values every time the button is clicked.
const nextBtn = document.querySelector('#slideNext');
const prevBtn = document.querySelector('#slidePrev');
const slider = document.querySelector('.boxes-container');
const slideWidth = 250;
// Slider initial margin
slider.style.marginLeft = "0";
nextBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) - slideWidth) + 'px';
checkMargin();
}
prevBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) + slideWidth) + 'px';
checkMargin();
}
function checkMargin() {
if (slider.style.marginLeft == "0") {
prevBtn.style.display = "none";
} else if (slider.style.marginLeft == "-1000px") {
nextBtn.style.display = "none";
} else {
nextBtn.style.display = "inline-block";
prevBtn.style.display = "inline-block";
}
}
There are other methods to make the DOM reactive, like MutationObserver. But it may be an overkill to this task.
You created the logic correctly. The problem is that you don't check the margin every time the button is clicked (javascript is not reactive by default).
You have to wrap the margin's logic into a function and then check the margin values every time the button is clicked.
const nextBtn = document.querySelector('#slideNext');
const prevBtn = document.querySelector('#slidePrev');
const slider = document.querySelector('.boxes-container');
const slideWidth = 250;
// Slider initial margin
slider.style.marginLeft = "0";
nextBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) - slideWidth) + 'px';
checkMargin();
}
prevBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) + slideWidth) + 'px';
checkMargin();
}
function checkMargin() {
if (slider.style.marginLeft == "0") {
prevBtn.style.display = "none";
} else if (slider.style.marginLeft == "-1000px") {
nextBtn.style.display = "none";
} else {
nextBtn.style.display = "inline-block";
prevBtn.style.display = "inline-block";
}
}
There are other methods to make the DOM reactive, like MutationObserver. But it may be an overkill to this task.
answered Nov 12 '18 at 2:37
Gustavo Lopes
673217
673217
add a comment |
add a comment |
The reason why it wasn't working was because I wasn't checking for the margins when the buttons were clicked.
I changed my code as follow:
const nextBtn = document.querySelector('#slideNext');
const prevBtn = document.querySelector('#slidePrev');
const slider = document.querySelector('.boxes-container');
const slideWidth = 250;
// Slider initial margin
slider.style.marginLeft = "0px";
function check() {
if (slider.style.marginLeft == "0px") {
prevBtn.style.display = "none";
} else if (slider.style.marginLeft == "-1000px") {
nextBtn.style.display = "none";
} else {
nextBtn.style.display = "inline-block";
prevBtn.style.display = "inline-block";
}
}
window.onload = check(); // Check the margins when the page is loaded
nextBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) - slideWidth) + 'px';
check();
}
prevBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) + slideWidth) + 'px';
check();
}
- Wrapped the margin logic inside of a
check()
function - Check this function on page load and everytime the buttons are clicked
Thanks to Gustavo for his precious help!
add a comment |
The reason why it wasn't working was because I wasn't checking for the margins when the buttons were clicked.
I changed my code as follow:
const nextBtn = document.querySelector('#slideNext');
const prevBtn = document.querySelector('#slidePrev');
const slider = document.querySelector('.boxes-container');
const slideWidth = 250;
// Slider initial margin
slider.style.marginLeft = "0px";
function check() {
if (slider.style.marginLeft == "0px") {
prevBtn.style.display = "none";
} else if (slider.style.marginLeft == "-1000px") {
nextBtn.style.display = "none";
} else {
nextBtn.style.display = "inline-block";
prevBtn.style.display = "inline-block";
}
}
window.onload = check(); // Check the margins when the page is loaded
nextBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) - slideWidth) + 'px';
check();
}
prevBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) + slideWidth) + 'px';
check();
}
- Wrapped the margin logic inside of a
check()
function - Check this function on page load and everytime the buttons are clicked
Thanks to Gustavo for his precious help!
add a comment |
The reason why it wasn't working was because I wasn't checking for the margins when the buttons were clicked.
I changed my code as follow:
const nextBtn = document.querySelector('#slideNext');
const prevBtn = document.querySelector('#slidePrev');
const slider = document.querySelector('.boxes-container');
const slideWidth = 250;
// Slider initial margin
slider.style.marginLeft = "0px";
function check() {
if (slider.style.marginLeft == "0px") {
prevBtn.style.display = "none";
} else if (slider.style.marginLeft == "-1000px") {
nextBtn.style.display = "none";
} else {
nextBtn.style.display = "inline-block";
prevBtn.style.display = "inline-block";
}
}
window.onload = check(); // Check the margins when the page is loaded
nextBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) - slideWidth) + 'px';
check();
}
prevBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) + slideWidth) + 'px';
check();
}
- Wrapped the margin logic inside of a
check()
function - Check this function on page load and everytime the buttons are clicked
Thanks to Gustavo for his precious help!
The reason why it wasn't working was because I wasn't checking for the margins when the buttons were clicked.
I changed my code as follow:
const nextBtn = document.querySelector('#slideNext');
const prevBtn = document.querySelector('#slidePrev');
const slider = document.querySelector('.boxes-container');
const slideWidth = 250;
// Slider initial margin
slider.style.marginLeft = "0px";
function check() {
if (slider.style.marginLeft == "0px") {
prevBtn.style.display = "none";
} else if (slider.style.marginLeft == "-1000px") {
nextBtn.style.display = "none";
} else {
nextBtn.style.display = "inline-block";
prevBtn.style.display = "inline-block";
}
}
window.onload = check(); // Check the margins when the page is loaded
nextBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) - slideWidth) + 'px';
check();
}
prevBtn.onclick = function() {
slider.style.marginLeft = (parseInt(slider.style.marginLeft, 0) + slideWidth) + 'px';
check();
}
- Wrapped the margin logic inside of a
check()
function - Check this function on page load and everytime the buttons are clicked
Thanks to Gustavo for his precious help!
answered Nov 12 '18 at 2:56
Moromain
456
456
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53255234%2fmargin-condition-in-javascript%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
What seems to be the problem with the last part? What is not working with the if/else statement?
– Jack Bashford
Nov 12 '18 at 2:24
Sorry I haven't been quite clear about that. The issue is that the buttons doesn't switch state meaning that you can keep on clicking it and adding margin.
– Moromain
Nov 12 '18 at 2:25
1
i think the problem is that your if statements are only run once, but not every time the margin has updated. I would advice you to add this block of code into a function and then run these checks every time either nextBtn or the prevBtn are being clicked (when the margin is being changed).
– Andresson
Nov 12 '18 at 2:28