Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
101 views
in Technique[技术] by (71.8m points)

html - JavaScript SlimScroll usage in Angular page?

I am trying to use SlimScroll in an Angular page as shown below:

First I added the js file to angular.json:

"scripts": [
  "src/bower_components/jquery/dist/jquery.min.js",
  "src/bower_components/bootstrap/dist/js/bootstrap.min.js",
  "src/bower_components/jquery-slimscroll/jquery.slimscroll.js",
]

Then I try to call the related method in html as shown below:

<div class="example">
  
</div>
  

<script type="text/javascript">
  $(function(){
    $(".example").slimScroll({
      size: '4px', 
      width: '100%', 
      height: '100%', 
      color: '#ff4800', 
      allowPageScroll: true, 
      alwaysVisible: true     
    });
  });
</script>

But there is nothing on the page. What I am doing wrong?

question from:https://stackoverflow.com/questions/65846868/javascript-slimscroll-usage-in-angular-page

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to put the slimscroll constructor code inside an ngOnInit. Whether it be for a specific *.component.ts or inside the app.component.ts.

ngOnInit(){
$(function(){
    $(".example").slimScroll({
      size: '4px', 
      width: '100%', 
      height: '100%', 
      color: '#ff4800', 
      allowPageScroll: true, 
      alwaysVisible: true     
    });
  });
}

The ngOnInit is invoked after the component's html is injected into the page.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...