Skip to content Skip to sidebar Skip to footer

How To Scroll The Content In The Left And Right Direction On Hitting An Arrow In Mobile View?

I have a fiddle which is working in a way that when I take it in the mobile view, the square boxes horizontally scroll. The CSS codes which I have used for that are: @media only sc

Solution 1:

For your problem you need a few things

  1. Set two buttons to set the listeners in your code
  2. Get the initial position so you can calculate the slide

    let divMain = $('.product-all-contents')[0]; let position = $(divMain).children().position().left; const slideAmount = 150;

This 3 variables will be the main values for the code

  1. Last make the listenes for the slide

    $('#arrow-right').click(function() { $(divMain).animate({ scrollLeft: position + slideAmount }, 500); position += slideAmount; })

Hope this helps :>

$(document).ready(function() {
  let divMain = $('.product-all-contents')[0];
  let position = $(divMain).children().position().left;
  const slideAmount = 150;

  $('#arrow-right').click(function() {
    $(divMain).animate({
      scrollLeft: position + slideAmount
    }, 500);
    position += slideAmount;
  })

  $('#arrow-left').click(function() {
    $(divMain).animate({
      scrollLeft: position - slideAmount
    }, 500);
    position -= slideAmount;
  })
});
.product-all-contents {
  background-color: #f0f0f0;
  width: 70%;
  margin: auto;
}

.product-contents {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
}

.product-contents.product {
  width: 10%;
  text-align: center;
  height: 150px;
  padding-top: 1%;
  padding-left: 1%;
  padding-right: 1%;
  border-style: solid;
  border-width: 3px;
  border-color: rgb(145, 147, 150);
  background-color: white;
  border-radius: 10px
}

.ipads {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
}

.tvs {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
}

.franchise-hub-text,
.cloud-based-text,
.business-analytics-text,
.tech-support-text,
.order-management-text,
.employee-management-text,
.white-label-text,
.brand-label-text,
.lead-tracking-text,
.custom-invoicing-text,
.goal-setting-text,
.customization-tools-text,
.royalty-calculator-text,
.email-marketing-text {
  width: 50%;
}

div.goal-setting,
div.customization-tools,
div.custom-invoicing,
div.lead-tracking,
div.email-marketing,
div.royalty-calculator,
div.brand-control,
div.franchisehubtv,
div.cloudbasedtextipad,
div.business-analytics,
div.tech-support,
div.employee-management,
div.order-management,
div.white-label {
  display: flex;
  margin-left: 15%;
  margin-right: 15%;
  align-items: center;
  background-color: #f0f0f0;
  padding: 2%;
  margin-bottom: 5%;
}

.product-quotes {
  display: block;
  padding: 20px11px;
  width: 90%;
  color: #3b3b3d;
  background: white;
  border-radius: 2px;
  line-height: 1.625;
  font-family: 'Roboto';
  font-weight: normal;
  "

}

.arrow-down {
  width: 0;
  height: 0;
  margin: auto;
  border-left: 40px solid transparent;
  border-right: 40px solid transparent;
  border-top: 40px solid #f0f0f0;
}

.white {
  display: none;
}

@mediaonly screen and (max-width: 767px) {
  .product-all-contents {
    overflow-x: auto;
  }
  .product-contents.product {
    min-width: 50.795%;
    margin: 02%;
    padding-top: 3.91%;
    padding-left: 3.91%;
    padding-right: 3.91%;
  }
}

@mediaonly screen and (max-width: 767px) {
  div.goal-setting,
  div.customization-tools,
  div.custom-invoicing,
  div.lead-tracking,
  div.email-marketing,
  div.royalty-calculator,
  div.brand-control,
  div.franchisehubtv,
  div.cloudbasedtextipad,
  div.business-analytics,
  div.tech-support,
  div.employee-management,
  div.order-management,
  div.white-label {
    display: inline-block !important;
  }
}

@mediaonly screen and (max-width: 767px) {
  .franchise-hub-text,
  .cloud-based-text,
  .business-analytics-text,
  .tech-support-text,
  .order-management-text,
  .employee-management-text,
  .white-label-text,
  .brand-control-text,
  .lead-tracking-text,
  .custom-invoicing-text,
  .goal-setting-text,
  .customization-tools-text,
  .royalty-calculator-text,
  .email-marketing-text {
    width: 100%;
  }
}
<!doctype html><html><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1"><title>Testing</title><linkrel="stylesheet"href="sample.css"><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><scripttype="text/javascript"src="script/myscript.js"></script></head><body><divclass="product-all-contents"><divclass="product-contents"><divclass="product"id="franchisehub"><pstyle=" font-size: 15px; font-family: 'Roboto'; margin-left: 7%; margin-right: 7%; line-height: 1.2; margin-top: 20%;
         color: rgb(58, 59, 60);">A</p></div><divclass="product"id="cloudbasedmobile"style="background-color:#81bf44;"><pstyle=" font-size: 15px;
         font-family: 'Roboto';line-height:1.2; margin-top: 27%;
         color:white;">Z</p></div><divclass="product"id="businessanalytics"><pstyle=" font-size: 15px;
         font-family: 'Roboto';line-height:1.2; margin-top: 22%;
         color: rgb(58, 59, 60);">B</p></div><divclass="product"id="techsupport"><pstyle=" font-size: 15px;
         font-family: 'Roboto';
         margin-right: 9%;
         line-height: 1.2;
         margin-left: 9%; margin-top: 22%;
         color: rgb(58, 59, 60);">C</p></div><divclass="product"id="ordermanagement"><pstyle=" font-size: 15px;
         font-family: 'Roboto';line-height:1.2; margin-top: 23%;
         color: rgb(58, 59, 60);">D</p></div><divclass="product"id="employeemanagement"><pstyle=" font-size: 15px;
         font-family: 'Roboto';line-height:1.2; margin-left: 5%; margin-top: 27%;
         color: rgb(58, 59, 60);">E</p></div><divclass="product"id="whitelabel"><pstyle="
         font-size: 15px;
         font-family: 'Roboto';
         line-height:1.2;
         margin-left: 14%;
         margin-right: 14%; margin-top: 8%;
         color: rgb(58, 59, 60);
         ">M</p></div></div><divclass="product-contents"><divclass="product"id="brandcontrol"><pstyle="
         font-size: 15px;
         font-family: 'Roboto';
         margin-left: 8%;
         line-height:1.2;
         margin-right: 8%; margin-top: 20%;
         color: rgb(58, 59, 60);
         ">F</p></div><divclass="product"id="leadtracking"><pstyle="
         font-size: 15px;
         font-family: 'Roboto';
         line-height:1.2;
         margin-left: 5%;
         margin-right: 5%; margin-top: 26%;
         color: rgb(58, 59, 60);
         ">G</p></div><divclass="product"id="custominvoicing"><pstyle=" font-size: 15px;
         font-family: 'Roboto';line-height:1.2; margin-top: 24%;
         color: rgb(58, 59, 60);">K</p></div><divclass="product"id="goalsetting"><pstyle="font-size: 15px;
         font-family: 'Roboto';
         margin-right: 13%;
         margin-left: 13%;
         line-height: 1.2; margin-top: 24%;
         color: rgb(58, 59, 60);">H</p></div><divclass="product"id="customizationtools"><pstyle=" font-size: 15px;
         font-family: 'Roboto';line-height:1.2; margin-top: 22%;
         color: rgb(58, 59, 60);">I</p></div><divclass="product"id="royaltycalculator"><pstyle=" font-size: 15px;
         font-family: 'Roboto';line-height:1.2; margin-left: 5%; margin-top: 23%;
         color: rgb(58, 59, 60);">J</p></div><divclass="product"id="emailmarketing"><pstyle="
         font-size: 15px;
         font-family: 'Roboto';
         margin-left: 5%;
         margin-right: 5%;
         line-height:1.2; margin-top: 21%;
         color: rgb(58, 59, 60);
         ">K</p></div></div></div><buttonid="arrow-left">&#60----</button><buttonid="arrow-right">----&#62</button></body></html>

Post a Comment for "How To Scroll The Content In The Left And Right Direction On Hitting An Arrow In Mobile View?"