PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Blocks

    Design blocks for your website

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

Question By
Solved

Question about horizontal scrolling bar

Solved
sunmoonidegu

Jun 11th, 2020 01:35 PM

Hello everyone,

This is my first post. I will try to describe my question as clear as possible.

Basically, my job is to combine two functions below:

Goal

  1. Generate multiple charts (click me). For this function,You can find the original question here (click me).

  2. Horizontal Scroll Bar (click me)

So what I want is to generate multiple charts inside the horizontal scroll bar. However, the data of charts comes from mysql database. Also, the number of chart will be different depends on the query. I finish this part, but the result doesn't go well.

Problems

The following are my problems:

  1. For example, my query result generate 20 charts. I can't see them on my browser. I think it is because the width of dragBar
<div id="dragBar"></div>

doesn't change. I have seen the jquery.horizontal.scroll.js file in Horizontal Scroll Bar (click me). I think the width of the dragBar should change depends on the number of chart in order to scroll through all of them, but I don't know how to sync the number of chart with the code inside the js file to change the width.

  1. In Horizontal Scroll Bar (click me), you can click on the track part.
<div id="track"></div>

Then the dragBar should move to the position you click. However, it doesn't work for me. When I click the track, my dragBar always go back to the left end. I don't know why.

I am also wondering if it is the problem with my css file. because my layout of Horizontal Scroll Bar (click me)

<ul id="horiz_container_outer"></ul>

is different from Horizontal Scroll Bar (click me) when I use the developer mode in safari.

I provide part of my code with database below to be clear.

<section id="ib">
    <div class="ib container">
        <div class="container-inside">
            <div class="search-box">
                <form>
                    <input type="text" name="q" id="search" placeholder="Search for company...">
                    <span></span>
                    <input type="submit" value="Submit" style="display: none;">
                    </form>
            </div>
        </div>
        <?php
            $servername = "localhost";
            $username = "root";
            $password = "root"; 
            $dbname = "xxx";
                    
            // Create connection
            $conn = new mysqli($servername, $username, $password, $dbname);
            // Check connection
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            } 

            $sql_cid = "select xxx from xxx where xxx=\"".$_GET['q']."\"";
            $result_cid = $conn->query($sql_cid);

            if (mysqli_num_rows($result_cid) > 0){
                while($row = mysqli_fetch_assoc($result_cid)) {
                    $cid = $row['U3_ID'];
                }
            }
            $sql = "select * from xxx where xxx=".$cid;
            $result = $conn->query($sql);
            $pds = array();
                    
            if (mysqli_num_rows($result) > 0) {
                while($row = mysqli_fetch_assoc($result)) {
                    $pds[] = array_values($row);
                }
                $count = count($pds);
            } else {
                echo "0 results";
            }
            ?>
            <script>
                window.onload = function() {
                        
                    var charts = []; 
                    var chartCount = <?php echo $count?>;
                    var maxCount = charts.length + Number(chartCount);
                    var all_data = <?php echo json_encode($pds, JSON_NUMERIC_CHECK); ?>;
                        
                    for(var i = charts.length; i < maxCount; i++){
                        var data = all_data[i];
                        var pd   = data.slice(2, data.length);
                        var date = data[2];
                        pd = pd.map(function (row, index) {
                                return {
                                    x: index,
                                    y: row
                                };
                            });
                        var li = document.createElement("li");
                        li.setAttribute("style", "width : 500px; height : 300px");
                        li.id = "horiz_container" + i;
                        document.getElementById("horiz_container").appendChild(li);

                        charts[i]= new CanvasJS.Chart("horiz_container"+i, {
                                zoomEnabled: true,
                                theme: "light2",
                                backgroundColor: "#F5DEB3",
                                title :{
                                  text: date
                                },
                                axisX:{
                                    title: "Time"
                                },
                                data: [{
                                  type: "line",
                                  dataPoints: pd
                                }]
                            });
                            charts[i].render();
                        }
                        document.getElementById("horiz_container").style.width =  "<?php echo 505*$count ?>px";
                        }
            </script>
            <div class="container-image">
                <ul id="horiz_container_outer">
                    <li id="horiz_container_inner">
                        <ul id="horiz_container">
                            <li></li>
                        </ul>
                    </li>
                </ul>
                <div id="scrollbar">
                    <a id="left_scroll" class="mouseover_left" href="#"></a>
                    <div id="track">
                        <div id="dragBar"></div>
                    </div>
                    <a id="right_scroll" class="mouseover_right" href="#"></a>
                </div>
            </div>
        </div>
    </section>

By the way, you can see I put

<li></li>

in

<ul id="horiz_container">

first. The reason is because if I don't do this, when I scroll the window of the horizontal scroll bar with my mouse, it scroll vertically, which I don't know why either... Hope you can help me out. I really want this to work. If you need more information, such as my css file or others. Please let me know. I will reply as soon as possible.

Thank you

tnylea

Jun 11th, 2020 04:41 PM

Hey sunmoon,

Thanks for posting the question. I want to try and clarify what you're trying to accomplish.

You basically want to have a set of charts (generated from some mysql data) displayed in a Horizontal view, which allows the user to scroll through the charts horizontally.

Is that correct?

Also, is jQuery a requirement, I seen you reference the jquery horizontal scrollbars, but this might actually be accomplished just using standard CSS and overflow correctly.

As you can see this menu has a horizontal scroll using standard CSS https://www.w3schools.com/howto/howto_css_menu_horizontal_scroll.asp

Let me know if you think the standard CSS horizontal scrolling will work and I'll see what I can do to help you out further.

Thanks! Talk to you soon.

Report
1
sunmoonidegu

Jun 11th, 2020 09:49 PM

Best Answer

Hello,

Yes, you are correct.

In fact, I am not sure whether jQuery is required. I just found these tools in order to make this function works.

The link you provided can only scroll when the mouse is clicking on the dragBar. However, I prefer to have all the functions in the website http://www.htmldrive.net/items/show/966/jQuery-Horizontal-automatic-Scrollbars-with-mouse, which are

  1. Scrolling the window by scrolling the wheel on my mouse.
  2. Clicking on the track will shifts the position of the dragBar to where I click on the track.
  3. When I hover on the left/right arrows on both side of the track, the window will constantly move in that direction.

The above three functions seem can not be done in the link you provided. I will say I need at least 1 and 2 to be done.

In order to have these functions, I think combining the two links Horizontal scrolling bar and Multiple charts may be a faster way (since the code is already provided).

However, I am not a specialist in these tools (jQuery/CSS/JS). So if you can help me with a pure CSS. That should also be great!

Thank you very much!

tnylea

Jun 17th, 2020 12:23 AM

Hey sunmoon,

It looks like this can definitely be accomplished using some simple CSS and javascript.

Take a look at this codepen

Let me know if you think that solution will work for you. Also, send me a private message and I'll see if I can help you integrate it into your project.

Thanks! Talk to you soon.

sunmoonidegu

Jun 17th, 2020 03:50 AM

Hello Tony, The codepen link you reply works well. After a little modify will be great.

1 (Important part). Can you help me to add a drag bar? (Maybe you mistook my reply. I hope I can have scrolling and a drag bar at least.) I think the importance of drag bar is to let my users know approximately how many images are there in the window, because I may have more than a hundred images in one query. Having a drag bar in this case will help the user go faster to the image they want.

  1. When I hover on the "left" div. Sometimes the container did scroll left slowly, but sometimes it went back to the left most immediately. I have check the code, but I didn't see any function writing about the latter part. I wonder if it's because of the code
  leftScrollInterval = setInterval(function(){
    if(scrollPos > 0){
      scrollPos -= 5;
    }

Or anything else? Or maybe because I am using safari?

  1. Is it possible to start with the left most image (which should be card 5 in your example)?

I sent you a private message too. Please check.

Thank you very much :) Talk to you soon.

tnylea

Jun 21st, 2020 10:18 PM

Hey man :)

I did create another pen that shows the scrollbars at the bottom. In order to accomplish this, I had to set the container to direction:rtl;, which means I had to change the functionality to the scrolling hover, take a look here:

There may be some things that we can do to get the scrolling behaving to function a little bit more smoothly, but you would just need to do some minor tweaks and playing around.

Let me know if this gets you one step closer to what you're trying to accomplish :)

Talk to you soon!

sunmoonidegu

Jun 22nd, 2020 01:36 AM

Hello Tony,

thank you for the great answer!

This one works great.

The only problem now is to get the sql command involved and make the number of charts depend on the sql result.

I am trying to integrating this function into my project on my own now.

However, can you let me know how can you help me on integrating this function if I can't figure it out by myself?

Thank you for the great answer!