gpt4 book ai didi

angularjs - 在状态更改 + 后退按钮上使用 angular ui-router 向左/向右滑动(用于移动设备)

转载 作者:行者123 更新时间:2023-12-04 08:52:09 25 4
gpt4 key购买 nike

我将 Angular ui-router 和 ng-animate 用于混合移动应用程序。所以我想在屏幕(或状态)之间使用移动标准转换。更深入地了解应用程序,向左滑动。返回(使用后退按钮):向右滑动。没什么特别的,可以使用普通的 ng-route。但是,这也适用于 ui-router。
其实,slide-left 工作正常,返回时出现问题。它在“原始”div 上应用类向左滑动,但在副本或 ng-animate div 上应用向右滑动。这导致动画“交叉”,不好。

那里的例子在我的情况下不起作用。

索引.html:

<div class="container">
<div ui-view class="view"></div>
</div>

在 main.css 中:
.slide-left.ng-enter,
.slide-left.ng-leave,
.slide-right.ng-enter,
.slide-right.ng-leave {
position: absolute;
top: 0px; right: 0; bottom: 0; left: 0;
overflow-x: hidden;
background: inherit;
-ms-transition: 5.5s ease-in-out;
-webkit-transition: 5.5s ease-in-out;
transition: 5.5s ease-in-out;
}


.slide-left.ng-enter {
z-index: 100;
-webkit-transform: translateX(100%);
transform: translateX(100%);
}

.slide-left.ng-enter.ng-enter-active {
-webkit-transform: translateX(0);
transform: translateX(0);
}

.slide-left.ng-leave {
z-index: 101;
-webkit-transform: translateX(0);
transform: translateX(0);
}

.slide-left.ng-leave.ng-leave-active {
-webkit-transform: translateX(-100%);
transform: translateX(-100%);
}

.slide-right.ng-enter {
z-index: 101;
-webkit-transform: translateX(-100%);
transform: translateX(-100%);
}

.slide-right.ng-enter.ng-enter-active {
-webkit-transform: translateX(0);
transform: translateX(0);
}

.slide-right.ng-leave {
z-index: 100;
-webkit-transform: translateX(0);
transform: translateX(0);
}

.slide-right.ng-leave.ng-leave-active {
-webkit-transform: translateX(100%);
transform: translateX(100%);
}

在我的 routes.js 中:
我在抽象 View 中设置了 ng-class="slide"。
 $stateProvider
.state('enrollments', {
abstract: true,
url: '/tutorProcessEnrollments',
template: '<div class="scroller" ui-view ng-class="slide" ></div>'
})
.state('enrollments.list', {
url: '',
templateUrl: 'views/tutorProcessEnrollments.list.html',
controller: 'TutorEnrollmentsCtrl'
})
.state('enrollments.enrollment', {
url: '/:enrollment_id',
templateUrl: 'views/tutorProcessEnrollments.enrollment.html',
controller: 'TutorEnrollmentCtrl'
})

在 App.js 中
.controller('MainCtrl', ['$scope', '$rootScope', '$window', '$location', '$state', function ($scope, $rootScope, $window, $location, $state) {

$scope.slide = 'slide-left';

// Implement own state.goTo functionality, to set the slide-left default on every menu action.
$rootScope.goTo = function (route, param) {
$scope.slide = 'slide-left';

$state.go(route, param);
};


// Function for going back in the history. The back button is hidden on items where no sub items/states\ are available.
$rootScope.back = function () {
if ($scope.slide === 'slide-left') {

$scope.slide = 'slide-right';

}

$window.history.back();

};

}]);

如您所见,我使用 $window.history.back() 导航回前一个屏幕/状态,如果当前方向为左,则将其设置为右,否则,我什么都不做。这个主 Controller 添加在 Body 标签中。
这就是在 enrollments.enrollment 状态时发生的情况,然后返回到enrollments.list 状态。
<div class="scroller ng-scope slide-left ng-animate ng-enter ng-enter-active" ui-view="" ng-class="slide" style=""><div ng-model="enrollment" class="ng-scope ng-pristine ng-valid">
<div class="scroller ng-scope slide-right ng-animate ng-enter ng-enter-active" ui-view="" ng-class="slide" style=""><div ng-model="enrollment" class="ng-scope ng-pristine ng-valid">

ng-class 不同步的问题是 solved由 ui-router 团队提供,但显然我做错了。
我也不喜欢我当前的解决方法来获取每次点击以在应用程序中更深入地导航。

那么:如何使用后退按钮向左/向右滑动并保持类(class)同步?

最佳答案

我已经尽力了

 var myapp = angular.module('myApp', ["ui.router", "ngAnimate", 'hmTouchEvents', 'ngSanitize'])
myapp.config(function($stateProvider, $urlRouterProvider, $animateProvider){
$animateProvider.classNameFilter(/ani-/);
$stateProvider
.state('anon',{
template:'<ui-view class="ani-ui-view" ng-class="abstractView"/>',
abstract:true
})
.state("anon.foo", {
url: "/foo",
templateUrl : '/views/statechange/resources/partials/foo.html',
controller : 'fooCtl'
})
.state("anon.bar", {
url: "/bar",
templateUrl : '/views/statechange/resources/partials/bar.html',
controller : 'barCtl'
});

$urlRouterProvider.otherwise("/foo");
})
.run(function($rootScope){
$rootScope.$on("$stateChangeStart", function(event, currRoute, prevRoute, rejection) {

});
})
.animation('.fade-in', function($timeout){
return {
enter : function(element, done){
element.css({
'opacity':0,
transition:'all 300ms'
});
$timeout(function(){
element.css({
'opacity':1
});
},0)
}
}
})
.animation('.show-bar', function($timeout) {
return {
enter : function(element, done) {
element.css({
transition:'all 300ms',
transform:'translate3d(100%, 0, 0)'
});

$timeout(function(){
element.css({
transform:'translate3d(0, 0, 0)'
});
},0);

},
leave : function(element, done) {
element.css({
transition:'all 300ms',
transform:'translate3d(0, 0, 0)'
});

$timeout(function(){
element.css({
transform:'translate3d(100%, 0, 0)'
});
},0);
},

// you can also capture these animation events
addClass : function(element, className, done) {},
removeClass : function(element, className, done) {}
}
})
.animation('.slide-foo', function($timeout) {
return {
enter : function(element, done) {
element.css({
transition:'all 300ms',
transform:'translate3d(-100%, 0, 0)'
});

$timeout(function(){
element.css({
transform:'translate3d(0, 0, 0)'
});
},0);

},
leave : function(element, done) {
element.css({
transition:'all 300ms',
transform:'translate3d(0, 0, 0)'
});

$timeout(function(){
element.css({
transform:'translate3d(100%, 0, 0)'
});
},0);
},

// you can also capture these animation events
addClass : function(element, className, done) {},
removeClass : function(element, className, done) {}
}
})

.controller('mainCtl',[
'$scope', '$state', '$rootScope', '$window',
function($scope, $state, $rootScope, $window){
$rootScope.abstractView = 'fade-in';


console.log('mainCtl');


}
])
.controller('fooCtl',[
'$scope', '$state', '$timeout', '$rootScope',
function($scope, $state, $timeout, $rootScope){

$scope.changeState = function(_state){
$rootScope.abstractView = 'show-bar';
$state.go('anon.bar');
}

$scope.tip="I am foo";
}
])
.controller('barCtl',[
'$scope', '$state', '$stateParams', '$timeout', '$rootScope', '$window',
function($scope, $state, $stateParams, $timeout, $rootScope, $window){
$timeout(function(){
$scope.barshow = true;
});
$scope.tip="I am bar";

$scope.goBack = function($event){
$rootScope.abstractView = 'show-bar';
$window.history.back();
}
}
]);

索引html
<body ng-controller="mainCtl">
<div class="ui-view-container">
<div ui-view></div>
</div>
</body>

foo html
<section ng-controller="fooCtl">
<div class="well">
{{tip}}
<div class="text-right">
<button class="btn btn-default" hm-tap="changeState('anon.bar')">to bar -&gt;</button>
</div>
</div>

酒吧 html
<section ng-controller="barCtl">
<div class="well">
<div class="text-left">
<button class="btn btn-info" hm-tap="goBack($event);">&lt;- back</button>
</div>
{{tip}}
</div>

一些CSS
.ui-view-container {
position:relative;
}
.ani-ui-view{
position: absolute;
left: 0;
top: 0;
width:100%;
}

我希望它能帮助你

关于angularjs - 在状态更改 + 后退按钮上使用 angular ui-router 向左/向右滑动(用于移动设备),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25494908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com