如何在另一个脚本之后运行脚本?

时间:2012-08-16 22:11:47

标签: javascript jquery html css

我有一个浏览器调整大小脚本:

$(document).ready(function() {
    $(window).on('resize', function() {
        if ($(this).height() <= 800) {
            $('.content').css('max-height', '500px'); //set max height
        } 
        else {
            $('.content').css('max-height', ''); //delete attribute
        }
    }).resize()
})

我希望在窗口调整大小后运行jscrollpane,因为那时我需要滚动条。在当前代码中,我只显示了常规滚动条。

$(function() {
    $('.scroll-pane').jScrollPane();
});

有没有办法在max height脚本完成后运行这个脚本?

2 个答案:

答案 0 :(得分:4)

<script type="text/javascript">
$(document).ready(function() {

   $(window).on('resize', function(){
     if ($(this).height() <= 800){
      $('.content').css('max-height', '500px'); //set max height
     } else{
      $('.content').css('max-height', ''); //delete attribute
   }

   $('.scroll-pane').jScrollPane(); //why not call it here?

 }).resize()
})
</script>

答案 1 :(得分:0)

你只是在这里调用方法:

$('.scroll-pane').jScrollPane();

.. JavaScript完全能够在单个脚本块中执行更多操作。您不必将每个操作视为单独的脚本。

所以只需将你的方法调用放在resize-event-handler中,例如:

$(window).on('resize', function(){
    if ($(this).height() <= 800){
        $('.content').css('max-height', '500px'); //set max height
    } else{
      $('.content').css('max-height', ''); //delete attribute
    }

    $('.scroll-pane').jScrollPane(); // just call it here!
}

..你已经完成了。

阅读一些关于JavaScript的文章可能会有用。或者一些书。

一些不错的起点:
http://www.w3schools.com/js/default.asp
http://www.codecademy.com/

享受!