简体   繁体   English

滚动按钮在jQuery中不起作用

[英]Scroll buttons not working in jQuery

I'm developing a timeline application like this . 我正在开发一个时间表应用程序是这样

For that purpose I created two buttons to scroll left and right. 为此,我创建了两个按钮以向左和向右滚动。 I had wrote this code in jQuery, but it is not working. 我已经在jQuery中编写了此代码,但无法正常工作。

Here is my code: 这是我的代码:

<script>
        var ctx=document.getElementById('myCanvas').getContext('2d');       
        var years=1,hline =5;

    ctx.strokeStyle = "#7A7A52";    
    ctx.lineWidth = 1;

//Scale
ctx.fillStyle="white";
ctx.fillRect(0,150,1350,50);
var startYr=1890;
    do{
        ctx.beginPath();
        ctx.moveTo(hline,150);
        ctx.lineTo(hline,173);
        ctx.stroke();
            ctx.font = "12px Arial";
            ctx.fillStyle="#7A7A52";    
            ctx.fillText (startYr, hline-15, 190);  

        for (var i=0;i<10;i++)
        {
            ctx.moveTo(hline,150);
            ctx.lineTo(hline,160);
            ctx.stroke();
            hline = hline + 6;
        }
        years = years+10;
        startYr = startYr +10;
 }while(years<=1000);
//ScaleEnd
var i=0;
 $(".right").click(function(){
  $("#myCanvas").scrollLeft(i+=100);    
  });
 $(".left").click(function(){
  $("#myCanvas").scrollLeft(i-=100);    
  });

and my CSS code is: 而我的CSS代码是:

#myCanvas
    {
        background-color: #C2C2BB;
        margin-left: 180px;
        overflow: hidden;
    }

But .right and .left buttons are not scrolling. 但是.right和.left按钮不会滚动。 Please suggest me wht i'm doing wrong! 如果我做错了,请建议我!

First of all look into your javascript console for errors. 首先,请检查您的JavaScript控制台中的错误。 If there aren't any errors, then it's okay. 如果没有任何错误,那就可以了。 Secondly, you could post your code somewhere on the web, that we could test it. 其次,您可以将代码发布到网络上的某个地方,以便我们对其进行测试。 This would help us to answer your question. 这将帮助我们回答您的问题。 Third, as I see you have posted a javascript code, however, you haven't closed it with </script> tag. 第三,据我所知,您已经发布了一个javascript代码,但是尚未使用</script>标签将其关闭。 And the most important part: from my opinion you are doing it wrong, you shouldn't echo your all application on canvas element, try to use seperated div's, like the timeline.js. 最重要的部分:我认为您做错了,您不应该在canvas元素上回显所有应用程序,而是尝试使用单独的div,例如timeline.js。 It would be much easier and better. 这将更加容易和更好。 Try to make a main div with fixed width, set the overflow to none and place some extra div's on that main div. 尝试制作具有固定宽度的主div,将溢出设置为无,然后在该主div上放置一些额外的div。 For example: 例如:

<div id='timeline'> 
  <div id='first-event'></div>
  <div id='second-event'></div>
</div>

Then use jQuery function scrollTo, to scroll through your main div with arrows. 然后使用jQuery函数scrollTo,用箭头滚动浏览主div。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM