This website requires JavaScript to deliver the best possible experience.
إعادة تصور تجربة الشاشة الكاملة مع Focus

إعادة تصور تجربة الشاشة الكاملة مع Focus

تعد صفحات الشاشة الكاملة وسيلة رائعة لجذب انتباه جمهورك. هل ترغب في تقديم عرض تقديمي يشكل أفكارك ويوضح النقاط التي تريد إبرازها؟ هل ترغب في تصميم صفحة الملف الشخصي بشكل يساعدك على إبراز أعمالك. Focus مُكون كموقع للتمرير خلال شاشة كاملة. تعلم كيف قمنا ببنائه، والوظيفة الخاصة بذلك.

إليك كيف بنينا Focus؟

يحتوي Focus على المتغيرات التالية بشكل أساسي:

  • Sections
  • CurrentSection
  • Slides
  • SlidesCount
  • ActiveSlide
  • CurrentSlides
  • isSliding
  • ScrollingValues

طريقة البدء

نبدأ بوظائف التهيئة لضبط بعض القيم البدائية.

initDots( )

يُستخدم في تهيئة نقاط التنقل الجانبية (side navigation dots)، التي تعرض قائمة بالنقاط ، واحدة لكل شريحة ، مع indicator إلى active slide الحالية.

initDots() {
 this.fragment = document.createDocumentFragment();
 const focusNav = document.createElement('ul');

 focusNav.classList.add('focus-nav');
 focusNav.insertAdjacentHTML('afterbeing', '<li><a class="focus-dit"></a></li>'.repeat(this.sections.length));

 this.fragment.appendChild(focusNav);
 document.body.appendChild(this.fragment);

 this.dots = Array.from(document.querySelectorAll('.focus-dot'));
 this.dots.forEach((dot, index) => {
   dot.addEventListener('click', () => {
     this.goToSection(index);
   }, false);
 });
}

initKeyboard( )

function التي تجعل Focus متاحًا من خلال لوحة المفاتيح، مما يسمح للمستخدم بالتنقل عبر الشرائح، سواء أفقيًا أو رأسيًا ، باستخدام أزرار الأسهم.

initKeyboard() {
 window.addEventListener('keydown', (event) => {
   if(event.defaultPrevented) {
     return;
   }

   switch(event.key) {
     case 'ArrowDown':
       this.nextSection();
       break;
     case 'ArrowUp':
       this.previousSection();
       break;
     case 'ArrowRight':
       this.slideRight();
       break;
     case 'ArrowLeft':
       this.slideLeft();
       break;
     default:
       return;
   }

   // Cancel the default action to avoid it being handled twice
   event.preventDefault();
 }, true)
}

initScroll ( )

هذا لمعالجة عجلة الماوس للتمرير من خلال شرائح مختلفة.

initScroll() {
 function handler(e) {
   event.preventDefault();
   // get scrolling value
   const value = e.wheelDelta || -e.deltaY || -e.detail;

   //get scrolling direction
   const delta = Math.max(-1, Math.min(1, value));

   if(this.scrollingValues.length < 99) {
     this.scrollingValues.shift();
   }

   this.scrollingValues.push(Math.abs(value));
   const scrollingStart = getAverage(this.scrollingValues, 50);
   const scrollingEnd = getAverage(this.scrollingValues, 10);

   if(scrollingEnd < scrollingStart) return;
   if(delta === 1) {
     this.previousSection();
     return false;
   }
   if(delta === -1) {
     this.nextSection();
     // Cancel default browser behavior
     return false;
   }
 }

 window.addEventListener('mousewheel', handler.bind(this), false);
 // firefox
 window.addEventListener('DOMMouseScroll', handler.bind(this), false);

}

طرق المساعد Helper methods

هنا طريقتين تقوم بإرجاع the array of slides وطولها. أنها تأتي في متناول اليدين في طرق التنقل navigation.

getSlides( )

تحويل (an array of slides).

getSlides() {
 return this.sections.map((section) => {
   return Array.from(section.querySelectorAll('.slide'));
 });
}

getSlidesCount( )

تحويل طول (array of slides).

getSlidesCount() {
 return this.sections.map((section) => {
   return Array.from(section.querySelectorAll('.slide')).length;
 })
}

طرق التنقل

هنا نحدد الطرق المسؤولة عن التنقل من خلال الشرائح (slides) والأقسام (sections) المختلفة.

nextSection( )

وهذا ينقلنا إلى الجزء التالي عموديًا.

nextSection() {
 if(this.currentSection >= (this.sections.length - 1)) return;
 this.goToSection(this.currentSection + 1);
}

previousSection( )

وهذا يأخذنا إلى القسم السابق.

previousSection() {
 if(this.currentSection <= 0) return;
 this.goToSection(this.currentSection - 1);
}

(goToSection(index

وتأخذ هذه الطريقة الindex ك argument خاص بها، وينتقل المستخدم إلى الشريحة slide المحددة بواسطة index.

goToSection(index) {
 if(this.isSliding) return;
 this.isSliding = true;

 this.el.style.transform = `translate3d(0, -${index * window.innerHeight}px, 0)`;
 this.sections[this.currentSection].classList.remove('is-active');
 this.dots[this.currentSection].classList.remove('is-active');
 this.currentSection = index;

 this.sections[this.currentSection].classList.add('is-active');
 this.dots[this.currentSection].classList.add('is-active');
 setTimeout(() => {
   this.isSliding = false;
 }, 500);
}

slideRight( )

الانتقال إلى slide التالي ناحية اليمين.

slideRight() {
 const currentSlideCount = this.slidesCount[this.currentSection];
 const currentSlide = this.currentSlides[this.currentSection];

 if(currentSlide < currentSlideCount - 1) {
   this.goToSlide(currentSlide + 1)
 }
}

slideLeft( )

الانتقال إلى slide التالي ناحية اليسار.

slideLeft() {
 const currentSlide = this.currentSlides[this.currentSection];

 if(currentSlide > 0) {
   this.goToSlide(currentSlide - 1);
 }
}

(goToSlide(index

يمكنك الانتقال إلى شريحة أفقية (horizontal slide) معينة ومحددة بواسطة:

goToSlide(index) {
 const currentSlideCount = this.slidesCount[this.currentSection];

 if(currentSlideCount === 0) return;
 if(this.activeSlide) this.activeSlide.classList.remove('is-acitve');

 this.activeSlide = this.slides[this.currentSection][index];
 this.activeSlide.classList.add('is-active');

 this.currentSlides[this.currentSection] = index;
 this.sections[this.currentSection].style.,marginLeft = `-${index * 100}%`
}

Utilities

ربما لاحظت بعض الطرق التي لم يتم تعريفها، وتم وضعها بحيث يمكن إعادة استخدامها في ملف مختلف، لتنظيم أفضل للكود.

select( )

طريقة بسيطة لتحديد عنصر DOM.

export function select(element) {
 if(typeof element === 'string') {
   return document.querySelector(element);
 }

 return element;
}

(getAverage(array, length

عن طريق حساب وارجاع المتوسط الرياضي لarray of values.

export function getAverage(array, length) {
 let sum = 0;
 const elements = array.slice(Math.max(array.length - length, 1));
 elements.forEach((value) => sum = sum + value);

 return Math.ceil(sum / length);
}

(getArray(length, value

هي طريقة تقوم بإرجاع array ذات طول معين وتعبئتها value argument التي تم تمريرها إليها.

export function getArray(length, value) {
 return new Array(length).fill(value);
}
يمكنك تحميل Focus عبر غيت هاب الخاص بنا. لا تتردد في المساهمة في المشروع مفتوح المصدر!

التعليقات

المزيد من المقالات