[AngularJS] Directive for top scroll bar

7155 단어
angular.directive('dblScroll', dblScroll)


dblSroll.$inject = [
    '$timeout'
];
function dblScroll($timeout) {
    return {
        restrict: 'A',
        transclude: true,
        scope: {
            dblScroll: '
        },
        template: `
            
ng-hide="!dblScroll" ng-style="outterStyle" style="height: 20px; position: absolute; left: 0; right: 0; top: -20px; overflow-y: hidden; overflow-x: auto;" >
 
`, link: function (scope, el) { function selectParent() { const dom = document.querySelector('[dbl-scroll-container]'); if (dom) { return dom; } return el.parent()[0]; } let wLsn, winLsn, tLsn; let firstTime = null; const table = el[0]; const target = selectParent(); if (scope.dblScroll) { const wrapper = document.querySelector('.dblscroll__div--wrapper'); const targetScrollHandler = () => { wrapper.scrollLeft = target.scrollLeft }; const wrapperScrollHandler = () => { target.scrollLeft = wrapper.scrollLeft }; const windowResizeHandler = () => { $timeout(() => { update(); }, 66) }; tLsn = target.addEventListener('scroll', targetScrollHandler); wLsn = wrapper.addEventListener('scroll', wrapperScrollHandler); winLsn = window.addEventListener('resize', windowResizeHandler); const update = () => { const scrollWidth = table.scrollWidth; const clientWidth = target.clientWidth; scope.innerStyle = {width: scrollWidth + 'px'}; scope.outterStyle = {width: clientWidth + 'px'}; }; scope.$watch(() => { if (!firstTime) { firstTime = target.scrollWidth + target.clientWidth; } return target.scrollWidth + target.clientWidth - firstTime; }, () => { $timeout(() => { update(); }, 66) }); scope.$on('$destroy', function(){ if (tLsn) { tLsn.removeEventListener('scroll', targetScrollHandler); } if (wLsn) { wLsn.removeEventListener('scroll', wrapperScrollHandler); } if(winLsn) { winLsn.removeEventListener('resize', windowResizeHandler); } }); } } } } export default dblScroll;

 

Using it:

  <div style="position: relative;" >
    <div style="width: 300px; height: auto; overflow-x: auto;" >
      <div style="width: 600px; height: 300px;" dbl-scroll=true>divdiv>
    div>
  div>

 
다음으로 전송:https://www.cnblogs.com/Answer1215/p/6878840.html

좋은 웹페이지 즐겨찾기