Mofizur
Mofizur

Reputation: 145

Scroll to a Div in KnockoutJS

I was trying to create the similar jquery scroll functionality to scroll to a DIV when pressed a button but didn't find any good solution to do that in KnockoutJS. Could you please suggest any approach?

$('html,body').animate({ scrollTop: $('#myDivWhereToBeScrolled').offset().top }, 1000);

Upvotes: 1

Views: 3527

Answers (2)

Mofizur
Mofizur

Reputation: 145

The process was correct but my project setting was somehow different and thus it was scrolling the wrong element. Found the correct css element to be scrolled and worked like this

$('.goportal-content-scroll').scrollTop(0);

Thanks

Upvotes: 1

Well Wisher
Well Wisher

Reputation: 1875

you need to call a function in your model and from there you need to add your code, something like

 <button data-bind="click: yourFunction">Scroll</button>

and in your model

this.yourFunction = function(){
               $('html,body').animate({ scrollTop: $('#myDivWhereToBeScrolled').offset().top }, 1000);
           }

let me know is that you need.

i have created a plunkr, please have a look http://plnkr.co/edit/J0dGlzJT8sr2PJ0U83FS?p=preview

Upvotes: 3

Related Questions