WeChat applet bullet screen function

Source: Internet
Author: User
This article mainly introduces the simple example of the small program bullet screen function. For more information, see 1. mini-program-implementation of bullet screens (no background)

The applet has just come out. There are many online demos, but it is not easy to find what you need. Today, I will share with you a bullet screen function.

:

My idea is this: first use The tag determines whether to enable the bullet screen. if it is enabled, the bullet screen text box and the launch button will appear, as well as the bullet screen mask layer.

Paste wxml and wxss code first.

The wxml code is as follows:

 
  
    
      
      
    
  
 
 
   
    
  
   
Bullet screen
  
 
  
    
      
      
     
       
   Launch   
   
 
 
  
    
   
    
{Item. barrage_shootText }}
    
  
 

The wxss code is as follows:

/* Pages/index. wxss */. slide-image {width: 100%;}/* operation of the bullet screen selection button */. barrage-Switch {position: absolute; bottom: 10px; right: 10px; z-index: 2;}/* operation in the bullet screen input box */. barrage-inputText {position: absolute; display: flex; background-color: # BFBFBF; width: 100%; height: 40px; flex-direction: row; nav-index: 2; justify-content: center; align-items: center; bottom: 10% ;}. barrage-input {background-color: greenyellow; width: 60%; height: 30px ;}. barrage-shoot {margin-left: 10px; width: 25%; height: 30px ;}. shoot {width: 100%; color: black;}/* bullet screen fly */. barrage-fly {z-index: 3; height: 80%; width: 100%; position: absolute; top: 0 ;}. barrage-textFly {position: absolute ;}

In this way, all the basic styles are implemented. Next we need to process the words on the bullet screen.

The text is moved from right to left, the top position of the text appears is random, and the left is the width of the screen. The timer is used to process the movement.

In addition, the font color is random. These functions are processed by js.

The js code is as follows:

Var barrage_style_arr = []; var barrage_style_obj = {}; var phoneWidth = 0; var timers = []; var timer; Page ({data: {imgUrls :[' http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg ',' http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg ',' http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg '], IndicatorDots: true, autoplay: true, interval: 3000, duration: 500, barrageTextColor: "# D3D3D3", barrage_inputText: "none", barrage_shoottextColor: "black", bind_shootValue: "", barrage_style: [], barragefly_display: "none" ,}, // Life cycle function -- listener page loading onLoad: function (options) {var that = this; // Obtain the screen width wx. getSystemInfo ({success: function (res) {that. setData ({barrage_phoneWidth: res. windowWidth-100 ,})} }) PhoneWidth = that. data. barrage_phoneWidth; console. log (phoneWidth) ;}, // whether to enable the bullet screen... barrageSwitch: function (e) {console. log (e); // first judge if (! E. detail. value) {// clear the bullet screen barrage_style_arr = []; // set the value of data this. setData ({barrageTextColor: "# D3D3D3", barrage_inputText: "none", barragefly_display: "none", barrage_style: barrage_style_arr,}); // clear timer clearInterval (timer );} else {this. setData ({barrageTextColor: "#04BE02", barrage_inputText: "flex", barragefly_display: "block",}); // enable the timer = setInterval (this. barrageText_move, 800) }}, // launch button shoot: function (e) {// random font color var textColor = "rgb (" + parseInt (Math. random () * 256) + "," + parseInt (Math. random () * 256) + "," + parseInt (Math. random () * 256) + ")"; // Set the horizontal position style of the bullet screen font // var textWidth =-(this. data. bind_shootValue.length * 0); // sets the vertical position style var barrageText_height = (Math. random () * 266; barrage_style_obj = {// textWidth: textWidth, barrageText_height: barrageText_height, barrage_shootText: this. data. bind_shootValue, barrage_shoottextColor: textColor, barrage_phoneWidth: phoneWidth}; barrage_style_arr.push (barrage_style_obj); this. setData ({barrage_style: barrage_style_arr, // bind_shootValue: "" // clear input box}) // the timer starts the bullet screen. // this. timer = setInterval (this. barrageText_move, 800) ;}, // The timer calls the bullet screen. barrageText_move: function () {var timerNum = barrage_style_arr.length; var textMove; for (var I = 0; I
 
  

Because I have just come into contact with small programs, I am not very familiar with the use of some statements. So I encountered some problems:

1. obtain wxml control information in js.

Js:

 barrageSwitch: function(e){  console.log(e); }

Wxml:

   

Result: an objec is returned. data of the json format is returned in the console.

 Object {type: "change", timeStamp: 2766, target: Object, currentTarget: Object, detail: Object}

2. when implementing the bullet screen, click the launch button to obtain the information in the input box.

In this case, when the input box loses the focus, I copy the data to the data Class in js, and then click "launch" to retrieve the value in the data class.

3. the biggest problem is how to run the text. because small programs do not support jQuery, this JavaScript idiot is powerless.

Let's talk about how to move text:

First, define a timer when the bullet screen is opened, and turn off the timer when the bullet screen is closed. Because I use arrays to store text movement styles, the rest are determined. I only need to change the left size to move the text. So I use the for loop. when the timer is running, the left: xxx px; text style of the bullet screen is changed.

Thank you for reading this article. I hope it will help you. thank you for your support for this site!

For more articles on the bullet screen function of small programs, please follow the PHP Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.