Javascript 垂直滚动在 Ionic 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27153528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vertical scrolling not working in Ionic
提问by CaffGeek
I have two ion-scrollelements on my page, the top one scrolls horizontally, below it is a list that should scroll vertically.
我的ion-scroll页面上有两个元素,顶部一个水平滚动,下面是一个应该垂直滚动的列表。
The problem is it doesn't. It just bounces back to the top of the list.
问题是它没有。它只是弹回到列表的顶部。
Example: http://codepen.io/CaffGeek/pen/LEVdVY
示例:http: //codepen.io/CaffGeek/pen/LEVdVY
I have found that if I set a height on the vertical ion-scroll it 'works' but this needs to work on multiple devices and I don't know what height to use. I'd prefer to not have to watch events, and recalculate the height each time. Anybody know how to fix this?
我发现如果我在垂直离子滚动上设置一个高度它“有效”,但这需要在多个设备上工作,我不知道要使用什么高度。我宁愿不必观看事件,而是每次都重新计算高度。有谁知道如何解决这个问题?
HTML
HTML
<html ng-app="ionicApp">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Ionic List Scroll Bug</title>
<link href="http://code.ionicframework.com/nightly/css/ionic.min.css" rel="stylesheet">
<script src="http://code.ionicframework.com/nightly/js/ionic.bundle.min.js"></script>
</head>
<body ng-controller="MyCtrl">
<header class="bar bar-header bar-positive">
<h1 class="title">Ionic List Scroll Bug</h1>
</header>
<ion-content class="has-header">
<ion-scroll delegate-handle="calendarScroll" direction="x">
<div class="row">
<div class="col col-20" ng-repeat="day in payPeriod.days">
<div class="row">
<div class="col">{{day.name}}</div>
</div>
<div class="row">
<div class="col">{{day.number}}</div>
</div>
</div>
</div>
</ion-scroll>
<ion-scroll delegate-handle="taskScroll" direction="y">
<ul class="list">
<li class="list-item" ng-repeat="task in tasks">
<div class="row">
<div class="col col-80">
<p>{{task.name}}</p>
</div>
<div class="col col-20">
<label class="item item-input">
<input type="text" placeholder="Hours" ng-value="task.time" />
</label>
</div>
</div>
</li>
</ul>
</ion-scroll>
</ion-content>
<ion-footer-bar align-title="right" class="bar-stable">
<div class="buttons">
<button class="button">Save</button>
</div>
<h1 class="title">Total hours 0.00</h1>
</ion-footer-bar>
</body>
</html>
JavaScript
JavaScript
angular.module('ionicApp', ['ionic'])
.controller('MyCtrl', function($scope) {
$scope.payPeriod = {
days: [{ name: 'Mon', number: 3 }, { name: 'Tue', number: 4 }, { name: 'Wed', number: 5 },
{ name: 'Thu', number: 6 }, { name: 'Fri', number: 7 }, { name: 'Sat', number: 8 }, { name: 'Sun', number: 9 }]
};
$scope.tasks = [
{name: 'task 1', time: 1.0 },
{name: 'task 2', time: 3.0 },
{name: 'task 3', time: 2.0 },
{name: 'task 4', time: 1.0 },
{name: 'task 5', time: 2.0 },
{name: 'task 6', time: 1.0 },
{name: 'task 7', time: 1.0 },
{name: 'task 8', time: 2.0 },
{name: 'task 9', time: 1.0 },
{name: 'task 0', time: 1.0 }
];
});
回答by Josh Burgess
Here's a simple solution:
这是一个简单的解决方案:
Codepen illustrating a simple fix
Codepen 说明了一个简单的修复
Here's the meat of what we're doing:
这是我们正在做的事情的主要内容:
ion-scroll[direction=x] {
width: 100vw;
}
ion-scroll[direction=y] {
height: 100vh;
}
That will allow your scrolls to work as expected in your Ionic app (and, in fact, this is what I do in mine as part of the CSS boilerplate).
这将使您的滚动条在您的 Ionic 应用程序中按预期工作(事实上,这就是我作为 CSS 样板的一部分所做的工作)。
GIF: in action on CodePen
GIF:在 CodePen 上运行


回答by Juergen Fink
Hybrid work-around for you to try:
混合变通方法供您尝试:
Maybe it's not the best solution, but at least allows for scrolling, for you to try, if you like:
也许这不是最好的解决方案,但至少允许滚动,供您尝试,如果您愿意:
Just wrap your whole page content into a <div>with absolute position and define whether to scroll y and/or x:
只需将整个页面内容包装成一个<div>绝对位置并定义是否滚动 y 和/或 x:
<div style='position:absolute; top:0; right:0; bottom:0; left:0; overflow-y:scroll; overflow-x: hidden'>
<!-- your content of page here .... -->
</div>
Worked for me. Browsers compatibility on Windows 10, using ionic-package for Meteor JS (without Angular JS):
为我工作。Windows 10 上的浏览器兼容性,使用 ionic-package for Meteor JS(无 Angular JS):
:-) shows scrollbar on: - FireFox - Windows 10 Edge - MS IE 11
:-) 显示滚动条: - FireFox - Windows 10 Edge - MS IE 11
:-( Does not show scrollbar, but it scrolls:- Safari - Chrome - Opera
:-( 不显示滚动条,但会滚动:- Safari - Chrome - Opera
Hope this helps a bit for some cases.
希望这对某些情况有所帮助。

