ผ่านอาร์เรย์การขยายการประกบสตริงสามารถนำไปสู่ปัญหาด้านประสิทธิภาพได้อย่างง่ายดาย
การคัดลอกรหัสมีดังนี้:
ฟังก์ชัน StringBuffer () {
นี้ .__ Strings__ = new Array ();
-
StringBuffer.prototype.append = function (str) {
สตริง. __. push (str);
คืนสิ่งนี้;
-
stringbuffer.prototype.toString = function () {
ส่งคืนสตริง. __ __. เข้าร่วม ("");
-
var buffer = new StringBuffer ();
buffer.append ("สวัสดี") ผนวก ("JavaScript");
var result = buffer.toString ();
การแจ้งเตือน (ผลลัพธ์); // สวัสดี JavaScript
รหัสที่มา: https://gist.github.com/hehongwei44/fe71f10e4d2d9295Aeab
ฟังก์ชั่นผู้ช่วยของตำแหน่งของแถบ Scroll Page Viewport
การคัดลอกรหัสมีดังนี้:
/*สองฟังก์ชั่นเพื่อกำหนดความสูงและความกว้างของหน้าปัจจุบัน*/
ฟังก์ชั่น pageHeight () {
ส่งคืน document.body.scrollheight;
-
ฟังก์ชัน pagewidth () {
ส่งคืน document.body.scrollwidth;
-
/*กำหนดตำแหน่งแนวนอนและแนวตั้งของแถบเลื่อน*//
ฟังก์ชั่น scrollx () {
var de = document.documentelement;
คืน self.pagexoffset || (de && de.scrollleft) || document.body.scrollleft;
-
ฟังก์ชั่น scrolly () {
var de = document.documentelement;
คืน self.pageyoffset || (de && de.scrolltop) || document.body.scrolltop;
-
/*สองฟังก์ชั่นเพื่อกำหนดความสูงและความกว้างของเบราว์เซอร์วิวพอร์ต*//
ฟังก์ชั่น windowheight () {
var de = document.documentelement;
Return Self.innerHeight || (de && de.clientheight) || document.body.clientheight;
-
ฟังก์ชั่น windowwidth () {
var de = document.documentelement;
Return Self.innerWidth || (de && de.clientWidth) || document.body.clientwidth;
-
รหัสที่มา: https://gist.github.com/hehongwei44/62907b9b7061d4dfadb
ฟังก์ชั่นที่ปรับความโปร่งใสขององค์ประกอบ
การคัดลอกรหัสมีดังนี้:
/* ฟังก์ชั่นเพื่อปรับความโปร่งใสขององค์ประกอบ*/
ฟังก์ชั่น setOpacity (elem, ระดับ) {
// เช่นการประมวลผลความโปร่งใส
ถ้า (elem.filters) {
elem.style.filters = 'alpha (opacity =' + ระดับ + ')';
} อื่น {
elem.style.Opacity = ระดับ / 100;
-
-
รหัสที่มา: https://gist.github.com/hehongwei44/87839cd3b8439aff6a3c
รับฟังก์ชั่นทั่วไปหลายอย่างสำหรับตำแหน่งเมาส์
การคัดลอกรหัสมีดังนี้:
/*ใช้ฟังก์ชันทั่วไปสองฟังก์ชั่นเพื่อรับตำแหน่งปัจจุบันของเมาส์ที่สัมพันธ์กับทั้งหน้า*//
ฟังก์ชั่น getx (e) {
E = E || window.event;
ส่งคืน E.Pagex || e.clientx + document.body.scrollleft;
-
ฟังก์ชั่น gety (e) {
E = E || window.event;
ส่งคืน E.Pagey || e.clienty + document.body.scrolltop;
-
/*สองฟังก์ชั่นที่ได้รับตำแหน่งเมาส์เทียบกับองค์ประกอบปัจจุบัน*/
ฟังก์ชั่น getElementx (e) {
return (e && e.layerx) || window.event.offsetx;
-
ฟังก์ชั่น getElementy (e) {
return (e && e.layery) || window.event.offsety;
-
รหัสที่มา: https://gist.github.com/hehongwei44/2732365BD42BAA491EF8
ชุดของฟังก์ชั่นที่ใช้แอตทริบิวต์ cssdisplay เพื่อเปลี่ยนการมองเห็นองค์ประกอบ
การคัดลอกรหัสมีดังนี้:
-
* ฟังก์ชั่นที่ใช้การแสดงผลเพื่อซ่อนองค์ประกอบ
-
ฟังก์ชั่นซ่อน (elem) {
var curdisplay = getstyle (elem, 'display');
if (curdisplay! = 'none') {
Elem. $ oldDisplay = CURDISPLAY;
-
elem.style.display = 'ไม่มี';
-
-
* ใช้การแสดงผลเพื่อแสดงฟังก์ชั่นขององค์ประกอบ
-
ฟังก์ชั่นแสดง (elem) {
elem.style.display = elem. $ olddisplay || -
-
รหัสที่มา: https://gist.github.com/hehongwei44/b4192af8227d756bfda6
ฟังก์ชั่นทั่วไปที่เกี่ยวข้องกับสไตล์
การคัดลอกรหัสมีดังนี้:
-
* รับแอตทริบิวต์สไตล์ (ชื่อ) ขององค์ประกอบที่ระบุ (elem)
-
ฟังก์ชั่น getStyle (elem, ชื่อ) {
// ถ้ามีอยู่ในสไตล์ [] แล้วมันก็ถูกตั้งค่าแล้ว (และปัจจุบันอยู่)
if (elem.style [ชื่อ]) {
return elem.style [ชื่อ];
-
// มิฉะนั้นให้ทดสอบวิธีการ IE
อื่นถ้า (elem.currentstyle) {
return elem.currentstyle [ชื่อ];
-
// หรือวิธี W3C
อื่นถ้า (document.defaultView && document.defaultView.getComputedStyle) {
name = name.replace (/(az)/g, "-$ 1");
NAME = NAME.TOLOWERCASE ();
var s = document.defaultView.getComputedStyle (elem, "");
ส่งคืน s && s.getPropertyValue (ชื่อ);
-
// มิฉะนั้นผู้ใช้จะใช้เบราว์เซอร์อื่น
อื่น {
คืนค่า null;
-
-
รหัสที่มา: https://gist.github.com/hehongwei44/9ABF63536ACCD0F2EEB7
รับความสูงและความกว้างปัจจุบันขององค์ประกอบ
การคัดลอกรหัสมีดังนี้:
-
* รับความสูงที่แท้จริงขององค์ประกอบ
* ดูฟังก์ชั่นด้านบนสำหรับ GetStyle การพึ่งพา
-
ฟังก์ชั่น getheight (elem) {
กลับ parseint (getStyle (elem, 'ความสูง'));
-
-
* รับความกว้างที่แท้จริงขององค์ประกอบ
* ดูฟังก์ชั่นด้านบนสำหรับการพึ่งพา getStyle
-
ฟังก์ชั่น getWidth (elem) {
กลับ parseint (getStyle (elem, 'width'));
-
รหัสที่มา: https://gist.github.com/hehongwei44/b524ff25991d99625eb2
ข้างต้นเป็นสคริปต์ JavaScript ที่ใช้กันทั่วไปที่ใช้ร่วมกันในบทความนี้ ฉันหวังว่าคุณจะชอบพวกเขา