Angular.js data-bind background images using media queries

后端 未结 1 363
名媛妹妹
名媛妹妹 2020-12-20 22:51

I have an Angular.js application with dynamically background images (should have data-binding for their URL). I also want to use css media queries in order to detect orienta

相关标签:
1条回答
  • 2020-12-20 23:29

    This is just a starting point solving your problem.

    You may use matchMedia: https://developer.mozilla.org/en-US/docs/Web/API/Window.matchMedia

    if (window.matchMedia("(min-width: 400px)").matches) {
        $scope.poster = 'small.png';
    } else {
        $scope.poster = 'big.png';
    }
    

    now you can use it in the html file:

    <div class="moments-preview-image" 
        ng-style="{'background-image': 'url('+poster+ ')'}"> ... </div>
    

    If your browser doesn't support this new API you may have a look on some interesting workarounds:

    http://wicky.nillia.ms/enquire.js/

    http://davidwalsh.name/device-state-detection-css-media-queries-javascript

    0 讨论(0)
提交回复
热议问题