How to ensure videos autoplay and play inline in a card carousel on mobile devices?

I am building a vertical card carousel where each card has a video background. It works perfectly on desktop browsers (videos autoplay and play inline as expected), but on mobile browsers the videos fail to autoplay reliably.

I have ensured the following:

HTML attributes: The muted, playsinline, and preload="auto" attributes are set on each <video> tag.
Explicit muting in JavaScript: I programmatically set video.muted = true for each video.
Autoplay attempt in JavaScript: Using video.play() after a user interaction or on page load.

Despite these efforts, videos don’t show until interacted with on a phone.

Autoplay: Ensure videos autoplay seamlessly on smaller devices.
Play inline: Prevent videos from opening in fullscreen on mobile.
Inactive cards: Ensure inactive cards show the first frame of the video as a still image while paused.

const cards = document.querySelectorAll('.card');
const body = document.body;
const elementsToChange = document.querySelectorAll('.elementor-button, .elementor-widget-container a img');
let currentIndex = 0;
let isScrolling = false;
let touchStartY = 0;
let touchEndY = 0;

const bodyColors = ['#191d1c', '#534d3f', '#959185', '#bdb8b4', '#bec5bd'];

// Funksjon for å oppdatere styling på spesifikke elementer
function updateElementStyles() {
  const activeCard = cards[currentIndex];
  const cardTitle = activeCard.querySelector('h2').textContent.trim();
  const highlightCards = ['Om oss', 'Kultur']; // Kortene som skal trigge effekten

  if (highlightCards.includes(cardTitle)) {
    // Endre utseendet for de valgte elementene
    elementsToChange.forEach((el) => {
      if (el.tagName === 'IMG') {
        el.style.filter = 'invert(1)'; // Øk lysstyrken for bilder
      } else {
        el.style.color = '#ffffff'; // Sett tekstfarge til hvit
        el.style.transition = 'color 0.5s ease, filter 0.5s ease'; // Glatt overgang
      }
    });
  } else {
    // Tilbakestill til standard utseende
    elementsToChange.forEach((el) => {
      if (el.tagName === 'IMG') {
        el.style.filter = 'invert(0)'; // Tilbakestill lysstyrken
      } else {
        el.style.color = ''; // Tilbakestill tekstfarge
      }
    });
  }
}

function updateCards() {
  cards.forEach((card, index) => {
    const relativeIndex = (index - currentIndex + cards.length) % cards.length;
    const video = card.querySelector('video');

    if (relativeIndex === 0) {
      card.className = 'card active';
      video.play();
      body.style.backgroundColor = bodyColors[index];
    } else {
      card.className =
        relativeIndex === 1 ? 'card behind' :
        relativeIndex === 2 ? 'card behind-2' :
        relativeIndex === 3 ? 'card behind-3' :
        relativeIndex === 4 ? 'card behind-4' :
        'card ahead';
      video.pause();
    }
  });

  updateElementStyles(); // Oppdater styling på de valgte elementene
}

function handleScroll(direction) {
  if (isScrolling) return;
  isScrolling = true;

  if (direction === 'down') {
    currentIndex = (currentIndex + 1) % cards.length;
  } else if (direction === 'up') {
    currentIndex = (currentIndex - 1 + cards.length) % cards.length;
  }

  updateCards();
  setTimeout(() => (isScrolling = false), 600); // Prevent rapid scroll/swipe
}

window.addEventListener('wheel', (e) => {
  handleScroll(e.deltaY > 0 ? 'down' : 'up');
});

// Touch events for mobile
window.addEventListener('touchstart', (e) => {
  touchStartY = e.touches[0].clientY;
});

window.addEventListener('touchend', (e) => {
  touchEndY = e.changedTouches[0].clientY;
  if (touchStartY > touchEndY + 50) {
    handleScroll('down'); // Swipe up
  } else if (touchStartY < touchEndY - 50) {
    handleScroll('up'); // Swipe down
  }
});

updateCards();
body {
  margin: 0;
  padding: 0;
  height: 100vh;
  overflow: hidden;
  font-family: "Neue Montreal", sans-serif;
  background: #f0f0f0;
  transition: background 0.5s ease;
}

.carousel {
  position: relative;
  perspective: 1000px;
  height: 100vh;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  overflow: hidden;
}

.cards {
  position: relative;
  height: 70%;
  width: 70%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.card {
  position: absolute;
  height: 100%;
  width: 100%;
  border-radius: 30px 30px 0 0;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
  transform-origin: center;
  transition: transform 0.5s ease;
  display: flex;
  overflow: hidden;
}

.card .c-cards__media {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
  overflow: hidden;
  background-size: cover;
  background-position: center;
}

.card .c-cards__media video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none;
}

.card .c-cards__media-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
}

.card a {
  text-decoration: none;
  color: inherit;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  position: relative;
  z-index: 3;
  padding-bottom: 40px;
}

.card h2,
.card h3,
.card h4 {
  color: white;
  text-transform: uppercase;
  margin: 10px 0;
  text-align: center;
}

.card h2 {
  font-size: 18px;
}

.card h3 {
  font-size: 120px;
}

.card h4 {
  font-size: 18px;
}

.card {
  transform: translateY(50px) scale(0.8);
}

.card.active {
  transform: translateY(0) scale(1);
  z-index: 5;
}

.card.behind {
  transform: translateY(-80px) scale(0.9);
  z-index: 4;
}

.card.behind-2 {
  transform: translateY(-150px) scale(0.8);
  z-index: 3;
}

.card.behind-3 {
  transform: translateY(-215px) scale(0.7);
  z-index: 2;
}

.card.behind-4 {
  transform: translateY(-275px) scale(0.6);
  z-index: 1;
}

.card.ahead {
  transform: translateY(300px) scale(0.9);
  z-index: 0;
}
<div class="carousel">
  <div class="cards">
    <a href="https://ozeta.loddo.no/om-oss/" class="card">
      <div class="c-cards__media">
        <video muted="true" loop playsinline preload="auto">
            <source src="https://ozeta.loddo.no/wp-content/uploads/2024/12/about.mp4" type="video/mp4">
        </video>
        <div class="c-cards__media-overlay"></div>
      </div>
      <div class="c-cards__content">
        <h2>Om oss</h2>
        <h3>About Us</h3>
        <h4>Learn More</h4>
      </div>
    </a>
    <a href="https://ozeta.loddo.no/kultur/" class="card">
      <div class="c-cards__media">
        <video muted="true" loop playsinline preload="auto">
          <source src="https://ozeta.loddo.no/wp-content/uploads/2024/12/culture.mp4" type="video/mp4">
        </video>
        <div class="c-cards__media-overlay"></div>
      </div>
      <div class="c-cards__content">
        <h2>Kultur</h2>
        <h3>Culture</h3>
        <h4>Explore More</h4>
      </div>
    </a>
    <a href="https://ozeta.loddo.no/prototype/" class="card">
      <div class="c-cards__media">
        <video muted="true" loop playsinline preload="auto">
          <source src="https://ozeta.loddo.no/wp-content/uploads/2024/12/prototyping.mp4" type="video/mp4">
        </video>
        <div class="c-cards__media-overlay"></div>
      </div>
      <div class="c-cards__content">
        <h2>Prototyping</h2>
        <h3>Prototype</h3>
        <h4>Discover</h4>
      </div>
    </a>
    <a href="https://ozeta.loddo.no/produksjon/" class="card">
      <div class="c-cards__media">
        <video muted="true" loop playsinline preload="auto">
          <source src="https://ozeta.loddo.no/wp-content/uploads/2024/12/production-1.mp4" type="video/mp4">
        </video>
        <div class="c-cards__media-overlay"></div>
      </div>
      <div class="c-cards__content">
        <h2>Produksjon</h2>
        <h3>Production</h3>
        <h4>Details</h4>
      </div>
    </a>
    <a href="https://ozeta.loddo.no/baerekraft/" class="card">
      <div class="c-cards__media">
        <video muted="true" loop playsinline preload="auto">
          <source src="https://ozeta.loddo.no/wp-content/uploads/2024/12/production-1.mp4" type="video/mp4">
        </video>
        <div class="c-cards__media-overlay"></div>
      </div>
      <div class="c-cards__content">
        <h2>Bærekraft</h2>
        <h3>Bærekraft</h3>
        <h4>Details</h4>
      </div>
    </a>
  </div>
</div>

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật