jQuery width function parameters not working
I'm using a code found from a post in stackoverflow to scroll a division horizontally. I can scroll the div for full width but can't scroll for a specific amount of width.
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width()
, "slow");
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>
Above code is working!
jQuery documentation says that width() function can get values as parameters but when I try like follows, the scrolling is not working.
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width(50)
, "slow");
);
How can I solve this?
javascript jquery html css
add a comment |
I'm using a code found from a post in stackoverflow to scroll a division horizontally. I can scroll the div for full width but can't scroll for a specific amount of width.
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width()
, "slow");
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>
Above code is working!
jQuery documentation says that width() function can get values as parameters but when I try like follows, the scrolling is not working.
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width(50)
, "slow");
);
How can I solve this?
javascript jquery html css
you cannot use like that. you should get width and then add ur value.. like.width() + 100
because.width()
function returns the width
– Manjunath
Oct 30 '18 at 13:23
1
Just remove the$(document).width()
entirely. eg.scrollLeft: 50
– Turnip
Oct 30 '18 at 13:24
And, you can removeevent.preventDefault()
since there is no default behavior from clicking adiv
.
– Scott Marcus
Oct 30 '18 at 13:25
The documentation doesn't say you canget
values with parameters. It says that you canset
values by passing parameters. You don't want to set thedocument.width()
here, you just want to scroll by an amount, so simply pass that amount without any mention ofwidth()
.
– Scott Marcus
Oct 30 '18 at 13:27
add a comment |
I'm using a code found from a post in stackoverflow to scroll a division horizontally. I can scroll the div for full width but can't scroll for a specific amount of width.
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width()
, "slow");
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>
Above code is working!
jQuery documentation says that width() function can get values as parameters but when I try like follows, the scrolling is not working.
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width(50)
, "slow");
);
How can I solve this?
javascript jquery html css
I'm using a code found from a post in stackoverflow to scroll a division horizontally. I can scroll the div for full width but can't scroll for a specific amount of width.
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width()
, "slow");
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>
Above code is working!
jQuery documentation says that width() function can get values as parameters but when I try like follows, the scrolling is not working.
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width(50)
, "slow");
);
How can I solve this?
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width()
, "slow");
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width()
, "slow");
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>
javascript jquery html css
javascript jquery html css
edited Nov 15 '18 at 15:30
Shiladitya
9,44692031
9,44692031
asked Oct 30 '18 at 13:21
user10540027
you cannot use like that. you should get width and then add ur value.. like.width() + 100
because.width()
function returns the width
– Manjunath
Oct 30 '18 at 13:23
1
Just remove the$(document).width()
entirely. eg.scrollLeft: 50
– Turnip
Oct 30 '18 at 13:24
And, you can removeevent.preventDefault()
since there is no default behavior from clicking adiv
.
– Scott Marcus
Oct 30 '18 at 13:25
The documentation doesn't say you canget
values with parameters. It says that you canset
values by passing parameters. You don't want to set thedocument.width()
here, you just want to scroll by an amount, so simply pass that amount without any mention ofwidth()
.
– Scott Marcus
Oct 30 '18 at 13:27
add a comment |
you cannot use like that. you should get width and then add ur value.. like.width() + 100
because.width()
function returns the width
– Manjunath
Oct 30 '18 at 13:23
1
Just remove the$(document).width()
entirely. eg.scrollLeft: 50
– Turnip
Oct 30 '18 at 13:24
And, you can removeevent.preventDefault()
since there is no default behavior from clicking adiv
.
– Scott Marcus
Oct 30 '18 at 13:25
The documentation doesn't say you canget
values with parameters. It says that you canset
values by passing parameters. You don't want to set thedocument.width()
here, you just want to scroll by an amount, so simply pass that amount without any mention ofwidth()
.
– Scott Marcus
Oct 30 '18 at 13:27
you cannot use like that. you should get width and then add ur value.. like
.width() + 100
because .width()
function returns the width– Manjunath
Oct 30 '18 at 13:23
you cannot use like that. you should get width and then add ur value.. like
.width() + 100
because .width()
function returns the width– Manjunath
Oct 30 '18 at 13:23
1
1
Just remove the
$(document).width()
entirely. eg. scrollLeft: 50
– Turnip
Oct 30 '18 at 13:24
Just remove the
$(document).width()
entirely. eg. scrollLeft: 50
– Turnip
Oct 30 '18 at 13:24
And, you can remove
event.preventDefault()
since there is no default behavior from clicking a div
.– Scott Marcus
Oct 30 '18 at 13:25
And, you can remove
event.preventDefault()
since there is no default behavior from clicking a div
.– Scott Marcus
Oct 30 '18 at 13:25
The documentation doesn't say you can
get
values with parameters. It says that you can set
values by passing parameters. You don't want to set the document.width()
here, you just want to scroll by an amount, so simply pass that amount without any mention of width()
.– Scott Marcus
Oct 30 '18 at 13:27
The documentation doesn't say you can
get
values with parameters. It says that you can set
values by passing parameters. You don't want to set the document.width()
here, you just want to scroll by an amount, so simply pass that amount without any mention of width()
.– Scott Marcus
Oct 30 '18 at 13:27
add a comment |
4 Answers
4
active
oldest
votes
$(document).width()
function provide the window width. That means $(document).width()
is equal to a value. Then you have to replace $(document).width()
with your value. That's how it work.
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: "100px"
, "slow");
);
add a comment |
Here you go with a solution
Documentation: https://api.jquery.com/scrollleft/
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: "50px"
, "slow");
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>
Hope this will help you.
add a comment |
That's because the construction $(document).width(50)
is not valid, you can just use a regular number, or the result of calculation instead. For example, these results are valid:
$(document).width()
50
$(document).width() - 50
Here is an example:
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width() - 1000
, "slow");
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
</div>
add a comment |
Reason
Reason why scrollLeft: $(document).width(50)
didn't work because $(document).width(50)
is not returning a valid number. (Since it is a setter method.)
Fixes
.scrollLeft( value ) Is expecting value of type: Number (An integer indicating the new position to set the scroll bar to.)
$("div").on("click", function(event)
event.preventDefault();
var ScrollValue = $(document).width() - 100;
$("html, body").animate(
scrollLeft: ScrollValue
, "slow");
);
ScrollValue
can be calculated accordingly to your requirement and be passed as an integer value.
Hope this solves your problem, For more info., refer: https://api.jquery.com/scrollleft/
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%2f53065263%2fjquery-width-function-parameters-not-working%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
$(document).width()
function provide the window width. That means $(document).width()
is equal to a value. Then you have to replace $(document).width()
with your value. That's how it work.
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: "100px"
, "slow");
);
add a comment |
$(document).width()
function provide the window width. That means $(document).width()
is equal to a value. Then you have to replace $(document).width()
with your value. That's how it work.
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: "100px"
, "slow");
);
add a comment |
$(document).width()
function provide the window width. That means $(document).width()
is equal to a value. Then you have to replace $(document).width()
with your value. That's how it work.
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: "100px"
, "slow");
);
$(document).width()
function provide the window width. That means $(document).width()
is equal to a value. Then you have to replace $(document).width()
with your value. That's how it work.
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: "100px"
, "slow");
);
answered Nov 9 '18 at 11:49
Dean JohnsDean Johns
1
1
add a comment |
add a comment |
Here you go with a solution
Documentation: https://api.jquery.com/scrollleft/
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: "50px"
, "slow");
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>
Hope this will help you.
add a comment |
Here you go with a solution
Documentation: https://api.jquery.com/scrollleft/
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: "50px"
, "slow");
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>
Hope this will help you.
add a comment |
Here you go with a solution
Documentation: https://api.jquery.com/scrollleft/
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: "50px"
, "slow");
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>
Hope this will help you.
Here you go with a solution
Documentation: https://api.jquery.com/scrollleft/
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: "50px"
, "slow");
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>
Hope this will help you.
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: "50px"
, "slow");
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: "50px"
, "slow");
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>
answered Oct 30 '18 at 13:25
ShiladityaShiladitya
9,44692031
9,44692031
add a comment |
add a comment |
That's because the construction $(document).width(50)
is not valid, you can just use a regular number, or the result of calculation instead. For example, these results are valid:
$(document).width()
50
$(document).width() - 50
Here is an example:
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width() - 1000
, "slow");
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
</div>
add a comment |
That's because the construction $(document).width(50)
is not valid, you can just use a regular number, or the result of calculation instead. For example, these results are valid:
$(document).width()
50
$(document).width() - 50
Here is an example:
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width() - 1000
, "slow");
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
</div>
add a comment |
That's because the construction $(document).width(50)
is not valid, you can just use a regular number, or the result of calculation instead. For example, these results are valid:
$(document).width()
50
$(document).width() - 50
Here is an example:
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width() - 1000
, "slow");
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
</div>
That's because the construction $(document).width(50)
is not valid, you can just use a regular number, or the result of calculation instead. For example, these results are valid:
$(document).width()
50
$(document).width() - 50
Here is an example:
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width() - 1000
, "slow");
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
</div>
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width() - 1000
, "slow");
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
</div>
$("div").on("click", function(event)
event.preventDefault();
$("html, body").animate(
scrollLeft: $(document).width() - 1000
, "slow");
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
</div>
edited Oct 30 '18 at 13:44
answered Oct 30 '18 at 13:26
Commercial SuicideCommercial Suicide
9,981103354
9,981103354
add a comment |
add a comment |
Reason
Reason why scrollLeft: $(document).width(50)
didn't work because $(document).width(50)
is not returning a valid number. (Since it is a setter method.)
Fixes
.scrollLeft( value ) Is expecting value of type: Number (An integer indicating the new position to set the scroll bar to.)
$("div").on("click", function(event)
event.preventDefault();
var ScrollValue = $(document).width() - 100;
$("html, body").animate(
scrollLeft: ScrollValue
, "slow");
);
ScrollValue
can be calculated accordingly to your requirement and be passed as an integer value.
Hope this solves your problem, For more info., refer: https://api.jquery.com/scrollleft/
add a comment |
Reason
Reason why scrollLeft: $(document).width(50)
didn't work because $(document).width(50)
is not returning a valid number. (Since it is a setter method.)
Fixes
.scrollLeft( value ) Is expecting value of type: Number (An integer indicating the new position to set the scroll bar to.)
$("div").on("click", function(event)
event.preventDefault();
var ScrollValue = $(document).width() - 100;
$("html, body").animate(
scrollLeft: ScrollValue
, "slow");
);
ScrollValue
can be calculated accordingly to your requirement and be passed as an integer value.
Hope this solves your problem, For more info., refer: https://api.jquery.com/scrollleft/
add a comment |
Reason
Reason why scrollLeft: $(document).width(50)
didn't work because $(document).width(50)
is not returning a valid number. (Since it is a setter method.)
Fixes
.scrollLeft( value ) Is expecting value of type: Number (An integer indicating the new position to set the scroll bar to.)
$("div").on("click", function(event)
event.preventDefault();
var ScrollValue = $(document).width() - 100;
$("html, body").animate(
scrollLeft: ScrollValue
, "slow");
);
ScrollValue
can be calculated accordingly to your requirement and be passed as an integer value.
Hope this solves your problem, For more info., refer: https://api.jquery.com/scrollleft/
Reason
Reason why scrollLeft: $(document).width(50)
didn't work because $(document).width(50)
is not returning a valid number. (Since it is a setter method.)
Fixes
.scrollLeft( value ) Is expecting value of type: Number (An integer indicating the new position to set the scroll bar to.)
$("div").on("click", function(event)
event.preventDefault();
var ScrollValue = $(document).width() - 100;
$("html, body").animate(
scrollLeft: ScrollValue
, "slow");
);
ScrollValue
can be calculated accordingly to your requirement and be passed as an integer value.
Hope this solves your problem, For more info., refer: https://api.jquery.com/scrollleft/
answered Oct 30 '18 at 13:52
Vishnu BaligaVishnu Baliga
367411
367411
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%2f53065263%2fjquery-width-function-parameters-not-working%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
you cannot use like that. you should get width and then add ur value.. like
.width() + 100
because.width()
function returns the width– Manjunath
Oct 30 '18 at 13:23
1
Just remove the
$(document).width()
entirely. eg.scrollLeft: 50
– Turnip
Oct 30 '18 at 13:24
And, you can remove
event.preventDefault()
since there is no default behavior from clicking adiv
.– Scott Marcus
Oct 30 '18 at 13:25
The documentation doesn't say you can
get
values with parameters. It says that you canset
values by passing parameters. You don't want to set thedocument.width()
here, you just want to scroll by an amount, so simply pass that amount without any mention ofwidth()
.– Scott Marcus
Oct 30 '18 at 13:27