Home  >  Article  >  Web Front-end  >  Practical development of JavaScript using DeviceOne (4) imitating Youku video application_javascript skills

Practical development of JavaScript using DeviceOne (4) imitating Youku video application_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:28:001249browse

Before you start development, you first need to consider the differences in the system. For example, does the iOS phone have a back button? Therefore, when developing, you must consider that the secondary decryption requires a back button, otherwise the iOS phone will fall into This page cannot be returned.

The Android system has a back button. In this case, the user needs to press the back button twice within 3 seconds before exiting the system, so as to prevent the user from accidentally pressing the back button. The specific code is implemented as follows:

[mw_shl_code=javascript,true]page.on("back", function(){
 if (canBack) {
 global.exit();
 } else {
 nf.toast("再按一次退出优酷");
 canBack = true;
 delay3.start();
 }
});
var delay3 = mm("do_Timer");
delay3.delay = 3000;
delay3.interval = 1000;
delay3.on("tick", function(){
 this.stop();
 canBack = false;
});[/mw_shl_code]

Imitate Youku Video to implement four main interfaces. You can switch between pages by sliding the page left or right, or you can switch between pages by clicking the bottom button, and the color of the bottom button will also change as the page switches. Change. This function is implemented through the slideview component, and the effect picture is as follows:

Bottom changes:

The code is implemented as follows:

[mw_shl_code=javascript,true]
function subChange(num,button,imgs,lbs)
{
    var strings = ["下载","频道","订阅","我的"];
    button.text = strings[num];
    var url = [
          "source://image/main/shouye",
          "source://image/main/pindao",
          "source://image/main/dingyue",
          "source://image/main/wode"
          ];
    for(var i = 0 ; i < 4 ; i++)
  {
      imgs.source = url;
      lbs.fontColor = "222222FF";
  }
  imgs[num].source += "b";
  lbs[num].fontColor = "0080FFFF";
  for(var i = 0 ; i < 4 ; i++)
  {
      imgs.source += ".png";
  }
    img.visible = false;
    bt.visible = false;
}
function indexChange(num,sv,button,imgs,lbs,img,bt)
{
    sv.set({index: num});
  sv.refreshItems();
  subChange(num,button,imgs,lbs);
}
do_button.on("touch",function(data, e){
    if(do_button.text == "下载")
    {
        app.openPage("source://view/download.ui","");
    }
});
//点击底部按钮实现主界面以及底部图片颜色的切换
var action_imgs = [ui("img_0"), ui("img_1"), ui("img_2"), ui("img_3")];
var action_lbs = [ui("lb_0"), ui("lb_1"), ui("lb_2"), ui("lb_3"),];
do_slideview_1.bindItems(listdata );
listdata.addData([
  {template : 0},
  {template : 1},
  {template : 2},
  {template : 3}
]);
do_slideview_1.refreshItems({});
var shouye = ui("shouye");
shouye.on("touch",function(data, e){
    indexChange(0,do_slideview_1,do_button,action_imgs,action_lbs,imageview,do_button_3);
});
var pindao = ui("pindao");
pindao.on("touch",function(data, e){
    indexChange(1,do_slideview_1,do_button,action_imgs,action_lbs,imageview,do_button_3);
});
var dingyue = ui("dingyue");
dingyue.on("touch",function(data, e){
    indexChange(2,do_slideview_1,do_button,action_imgs,action_lbs,imageview,do_button_3);
});
var wode = ui("wode");
wode.on("touch",function(data, e){
    indexChange(3,do_slideview_1,do_button,action_imgs,action_lbs,imageview,do_button_3);
});
//滑动主界面实现底部图片的切换
do_slideview_1.on("indexChanged",function(data, e){
    subChange(data,do_button,action_imgs,action_lbs);
});
[/mw_shl_code]

Switching between homepage posters is similar to switching between pages, but switching between posters also implements the function of automatic timing switching, switching every three seconds. Time control is implemented by do_timer. When using this component, the time period It cannot be 100, otherwise the ios system will have incompatibility issues. The picture and code are as follows:

Picture:

Code:

[mw_shl_code=javascript,true]
var ind = 0;
timer.delay = 0;
timer.interval = 1000;
timer.start({});
timer.on("tick", function(){
    DURATION++;
  if(DURATION == 3){
      ind =(ind+1)%4
      movieview.set({index: ind});
      movieview.refreshItems();
      DURATION = 0;
  }
});[/mw_shl_code]

Click the download button in the upper left corner of the main interface to enter the download interface. After clicking download, the application will download another picture poster from the Internet to replace the original poster. The download progress is displayed through the progress bar, and the download function is through the http component. The download method is implemented, and the progress bar is implemented through the do_progressbar component. The rendering and code implementation are as follows:

Effect:

Code:

[mw_shl_code=javascript,true]
var http = mm("do_Http");
http.timeout = 30000;
http.contentType = "application/json";
http.url = "http://h.hiphotos.baidu.com/baike/c0%3Dbaike60%2C5%2C5%2C60%2C20%3Bt%3Dgif/sign=88e9903f8735e5dd8421ad8d17afcc8a/f9198618367adab48dc66b2e89d4b31c8701e44d.jpg";
http.on("success", function(data) {
    do_imageview_1.source="data://xiazai.png";
    do_button_2.text = "下载成功";
});
http.on("progress", function(data) {
  do_ProgressBar_0.progress = data.currentSize * 100 / data.totalSize;
  lb_progress.text = data.currentSize * 100 / data.totalSize + "%";
});
do_button_2.on("touch",function(data, e){
    http.download("data://xiazai.png");
});[/mw_shl_code]

There are three buttons in the upper right corner. After clicking the first button from the left, a search interface will pop up. The search box of the search interface is implemented by the textfield component.

After clicking the second button, the interface for scanning the QR code will pop up. This interface implements the function of scanning the QR code. After successful scanning, the user will be prompted that the scan is successful. The QR code is implemented by the do_BarcodeView component. The implementation picture and implementation code are as follows Shown:

Code:

[mw_shl_code=javascript,true]var nf = sm("do_Notification");
//根据ID获取BarcodeView实例对象;
var barcode = ui("do_BarcodeView_1");
start();
function start(){
    //开始启动扫描
    barcode.start(function(data, e) {
        //扫描成功,执行回调函数
        var result = JSON.stringify(data); 
        nf.alert("扫描二维码成功!", "完成")
    });
}
var btn = ui("btn_restart");
btn.on("touch", function(data, e) {
    start();
})
[/mw_shl_code]

After clicking the third button, a picture will pop up on the interface. After pressing the button again, the picture will disappear. The effect is as follows:

Video playback function

Click on any poster on the homepage to enter the video playback interface. Click the play button to play the video. This function is implemented by the do_VideoView component. The effect and code are as follows:

Code:

[mw_shl_code=javascript,true]var page = sm("do_Page");
var app = sm("do_App");
var do_VideoView_1 = ui("do_VideoView_1");
var do_Button_1 = ui("do_Button_1");
var do_Button_2 = ui("do_Button_2");
var do_Button_3 = ui("do_Button_3");
page.on("back", function(){ app.closePage() });
ui("action_back").on("touch", function(){ app.closePage() });
do_Button_1.on("touch", function(data, e) {
    if(do_Button_1.text == "播放")
    {
        do_VideoView_1.play(0);
        do_Button_1.text = "暂停";
    }
    else if(do_Button_1.text == "暂停")
    {
        do_VideoView_1.pause();
        do_Button_1.text = "继续";
    }
    else if(do_Button_1.text == "继续")
    {
        do_VideoView_1.resume();
        do_Button_1.text = "暂停";
    }
});
do_Button_2.on("touch", function(data, e) {
    do_Button_1.text = "播放";
    do_VideoView_1.stop();
});[/mw_shl_code]

Click the "Poke, everything you like to see is here" button, and the main page will jump directly to the subscription page. The implementation of this function relies on data exchange between pages. The page component is used to define an event1 event in the logic code of the index page, and the fire function of the page component is called in the logic code of the shouye page to trigger the event1 event.

The rendering is as follows:

The logic code is as follows:

index page:

[mw_shl_code=javascript,true]page.on("event1", function(data, e) {
    indexChange(2,do_slideview_1,do_button,action_imgs,action_lbs,imageview,do_button_3);
});[/mw_shl_code]
shouye页面:
[mw_shl_code=javascript,true]do_Button_0.on("touch",function(data, e)
{
    page.fire("event1","");
});[/mw_shl_code]

The above is the practical development of JavaScript using DeviceOne (4) imitating Youku video application introduced by the editor. I hope you like it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn