Home > Back-end >  Why I cannot change the left attribute value for my html element?
Why I cannot change the left attribute value for my html element?

Time:01-04

I am trying change the left value for my img-list when clicking on the pointer I have made. After changing the left value, I can switch to another img.

I have state the absolute position for my img-list. And, when I click on the pointer, the left value has changed, the image has indeed changed to another image, but only for a sec. After a sec, it just changed back to the original image, and when I alert to see the current value of img_list.style.left, it is showing the correct value. But the left value still remains the same 0 value. I am not sure what's wrong in here, can someone give me some hints plz.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../reset.css">
    <style>
        .wrapper{
            width: 370px;
            height: 350px;
            background-color: #bfa;
            margin: 50px auto;
            padding:10px 0;
            overflow: hidden;
            position: relative;
        }
        .img-list{
            position: absolute;
            left: 0;
        }
        .img-list li{
            float:left; 
            margin:0 10px;
        }
        img{
            width:350px;
            height: 350px;
        }
        .pointer{
            position: absolute;
            bottom: 10px;
            left: 50%;
            transform: translateX(-50%);
        }
        .pointer a{
            float: left; 
            width: 15px;
            height: 15px;
            background-color: red;
            opacity: 0.5;
        }
        .pointer a:hover{
            background-color: grey;
        }
        .pointer a:not(:first-child){
            margin-left: 10px;
        }
    </style>
    <script type="text/javascript">
        window.onload = function () {
            var img_list = document.getElementById("imglist");
            var img = img_list.getElementsByTagName("img");
            img_list.style.width = 370 * img.length   "px";
            console.log(img_list.style.width);

            var pointer = document.getElementsByClassName("pointer")[0];
            var allA = pointer.getElementsByTagName("a");

            for (var i = 0; i < allA.length; i  ) {
                allA[i].index = i;
                allA[i].onclick = function () {
                    var value = this.index * (-370)   "px";
                    img_list.style.left = value;
                    alert(img_list.style.left);
                };
            }
        }
    </script>
</head>
<body>
    <div >
        <ul  id = "imglist">
        
            <li><a href=""><img src="./pics/01/d56283cb25574af8.jpg!cc_320x320.webp" alt=""></a></li>
        
            <li><a href=""><img src="./pics/03/eb1b74d5j00rmlivy000jc000go00b4c.jpeg" alt=""></a></li>
            <li><a href=""><img src="./pics/01/fef85dc4d0992e62.jpg!cc_320x320.webp" alt=""></a></li>
            <li><a href=""><img src="./pics/01/3bc798a7387a216d.jpg!cc_320x320.webp" alt=""></a></li>
        </ul>
        <div >
            <a href=""></a>
            <a href=""></a>
            <a href=""></a>
            <a href=""></a>
        </div>
    </div>
</body>
</html>

CodePudding user response:

it seems that the a elements with empty href attribute refreshes the page each time it is clicked, just remove them:

<div >
   <a></a>
   <a></a>
   <a></a>
   <a></a>
</div>
  • Related