如何实现前进和后退功能,例如浏览器 [英] how to implement back and forward functionality like browser
问题描述
我想实现像回到了功能和前锋在我的项目相同的浏览器,如网页我们在屏幕上。
我试过是
intially中号设置 currentscreenindex = -1
I want to implement functionality like back and forward in my project same as browser , like web page we have the screen.
what i tried is
intially m setting the currentscreenindex=-1
和在第一屏的数据来执行此功能
and when data of first screen comes execute this function
In screen data arrive function (){
this.currentscreenindex++;
this.screenvisited[this.currentscreenindex]=data;
}
下面是我的背部功能是我的尝试:
Here is my back function what i tried :
back(){
currentscreenindex--;
var screen=screenvisited[currentscreenindex];
// will go to this screen
}
但我的问题是这些是:
but what my problem is in these is :
currentscreenindex screen data in screenvisted array
When 1st screen arrived 0 Data of 1st screen
When 2st screen arrived 1 Data of 2st screen
When 3st screen arrived 2 Data of 3st screen
When 4st screen arrived 3 Data of 4st screen
When 5st screen arrived 4 Data of 5st screen
when user click back button 3 call the screen is set to 4th gib,
因此,当第四屏数据到货,目前指数将得到增量, CURRENTINDEX = 4
和数据在当前索引4将成为第四屏的,我认为这是不正确的,因为如果在 CURRENTINDEX = 4
数据将得到改变,那么我不能这样做前锋,如果是的话请告诉我如何纠正呢?
so when data of 4th screen arrive , the current index will get incremented so currentindex=4
and data at current index 4 will be of 4th screen , i think it is not correct , because if data at currentindex=4
will get changed then i can't do forward , if yes pls tell how can i correct it ?
请建议我怎么能写正确的前进和后退的功能做相同的功能的浏览器。
please suggest how can i write the proper back and forward function to do same functionality as browser .
推荐答案
我不知道你如何适应这个到你如何目前正在做,但你可以实现这样的功能:
I don't know how you'd fit this into how you're currently doing it, but you can achieve the functionality like this:
您有两个堆栈。背堆栈和一个堆栈。他们都开始了为空。当用户通过链接或航行的其他方法,除了后退/前进导航到一个新的页面,清除了下一个堆栈,当前网页添加到后退堆栈,并继续航行。当用户点击后面,将当前页面上的正向堆栈,从后面栈中弹出一个页面,并导航到弹出页面。当用户点击向前,将当前页面上的后退堆栈,从正向栈中弹出一个页面,并导航到弹出页面。
You have two stacks. A back stack and a next stack. They both start out being empty. When the user navigates to a new page through a link or some other method of navigation besides back/forward, clear the next stack, add the current page to the back stack, and continue navigation. When the user clicks back, push the current page onto the forward stack, pop a page from the back stack, and navigate to the popped page. When the user clicks forward, push the current page onto the back stack, pop a page from the forward stack, and navigate to the popped page.
编辑::您问一些伪code,所以在这里你走:
You asked for some pseudocode, so here you go:
var currentPage=null;
var backStack=[];
var forwardStack=[];
function navigate(newPage) {
currentPage=newPage;
// stub
}
function linkClicked(page) {
backStack.push(currentPage);
forwardStack=[];
navigate(page);
}
function goBack() {
forwardStack.push(currentPage);
navigate(backStack.pop());
}
function goForward() {
backStack.push(currentPage);
navigate(forwardStack.pop());
}
这篇关于如何实现前进和后退功能,例如浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!