2017-07-26 72 views
0

我使用這段代碼在搜索面板摺疊並展開時刷新/調整主窗口的大小。目前,Chrome,IE 11和Edge都可以很好地工作,但我擔心這會在其他瀏覽器中失敗。找到一個相當於jQuery的

因此,在我的index.html文件中,我添加了這裏,我添加了refreshWindow()ng單擊<div>元素。

<div class="cg-master-main-wrapper"> 
    <!-- slide left/expand sidebar --> 
    <div class="cg-master-toggle-bar" ng-if="sidebar.left && sidebar.collapsible" ng-click="sidebar.hidden=!sidebar.hidden; refreshWindow()" ng-class="{'closed':sidebar.hidden, 'open':!sidebar.hidden}"> 
     <button class="cg-toggle-arrow" type="button"> 
      <span class="icon icon-1-75x" ng-class="{'icon-chevron-right':sidebar.hidden, 'icon-chevron-left':!sidebar.hidden}"></span> 
     </button> 
    </div> 

    <!-- main Content --> 
    . . Other code . . 
</div> 

在我cgSessionController.js文件,我創建了refreshWindow()函數

angular.module("cgCore").controller("cgSessionController", ["$scope", "$location", "$log", "$q", "$window", "$rootScope", "Idle", "Keepalive", "cgSessionService", "cgGlobals", "$timeout", "cgNavigationService", "cgBoHelper", 
function ($scope, $location, $log, $q, $window, $rootScope, Idle, Keepalive, cgSessionService, cgGlobals, $timeout, cgNavigationService, cgBoHelper) 
{ 
    . .Some Code. . 

    $scope.refreshWindow = function() { 
     $timeout(function() { 
      var evt = document.createEvent('HTMLEvents'); 
      evt.initEvent('resize', true, true); 
      window.dispatchEvent(evt); 
     }); 
    } 

    . . Some More Code. . 
}]); 

如何使用jQuery來產生同樣的結果?

我試過使用$(window).trigger('resize'),但是當我摺疊並展開搜索面板時,這並不起作用。

任何想法都會很棒。 非常感謝。

+0

可以告訴你更多的代碼,你的一些HTML和在那裏你調用$(窗口).trigger( '調整') –

回答

0

可以jQuery trigger

// Listen to resize 
 
$(window).resize(function() { 
 
    console.log('resize') 
 
}) 
 

 
// Fire resize event manually, also you can resize the window to trigger the event! 
 
var e = jQuery.Event("resize"); 
 
jQuery(window).trigger(e);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

謝謝你塔裏克,但你的解決方案沒有幫助。 –