How do I let the bottom of the page degrade with Javascript?

Asked

Viewed 4,983 times

10

It would be something like the image below; I have no idea how to do this.

I don’t want to use image, I want to generate with code.

  • 1

    Have you tried using the property background: linear-gradient(cor1, cor2) of css?

2 answers

15

With pure CSS

As an alternative suggestion to what was asked, follows a much simpler solution, which dispenses with the use of JS, with a pure and relatively simple CSS:

body {
   background-image: -ms-linear-gradient(top, #FF3300 0%, #EEFF00 100%);
   background-image: -moz-linear-gradient(top, #FF3300 0%, #EEFF00 100%);
   background-image: -o-linear-gradient(top, #FF3300 0%, #EEFF00 100%);
   background-image: -webkit-gradient(linear, top, bottom, color-stop(0, #FF3300), color-stop(1, #EEFF00));
   background-image: -webkit-linear-gradient(top, #FF3300 0%, #EEFF00 100%);
   background-image: linear-gradient(to bottom, #FF3300 0%, #EEFF00 100%);
   /* conforme sugerido pelo @PapaCharlie, seguem soluções pra IE mais velho:
   /*IE7-*/ filter: progid:DXImageTransform.Microsoft.Gradient(
      startColorStr='#FF3300', endColorStr='#EEFF00', GradientType=0);
   /*IE8+*/ -ms-filter: "progid:DXImageTransform.Microsoft.Gradient(
      startColorStr='#FF3300', endColorStr='#EEFF00', GradientType=0)";
}

Upshot:

Gradient

Run your tests on Jsfiddle, or use this tool to automatically generate the CSS for you.

With JS

I’m not going to repeat the code that is already in @brasofilo’s solution (which has already received my vote as well), because I’m basically complementing the idea he presented, with a way to use the gradient solution presented as a background image. For this, this method can be used:

Canvas.toDataURL("image/png")

Application example in Jquery:

$('body').css({'background-image':"url(" + Canvas.toDataURL("image/png")+ ")" });


Beware of certain solutions supposedly in "Javascript" by the web. While researching external alternatives, I noticed that many of them simply generate a CSS, and what’s worse, inferior to the one posted in the reply.

  • It’s all right, it’s all right.

9

The great onetrickpony has an example using the HTML element canvas. According to the documentation, the element is supported by Firefox 1.5+, IE 9+, Chrome and Opera 9. For versions smaller than IE 9, it is necessary to use the library Explorer Canvas.

HTML

<canvas id="gradient"></canvas>

CSS

canvas#gradient {
    display: block;
    height: 100%;
    width: 100%;
}

JS

var
  canvas = document.getElementById('gradient'),
  context = canvas.getContext('2d'),
  gradient = context.createLinearGradient(0, 0, 0, canvas.height);

gradient.addColorStop(0, '#ffffaa');
gradient.addColorStop(1, '#770000');  

context.fillStyle = gradient;
context.fillRect(0, 0, canvas.width, canvas.height);

Jsfiddle

  • 3

    As always in ie does not work :-|

  • @test, there in the manual indicates to use the library I added in the reply to IE.

  • In IE 9 canvas works but -ms-linear-gradient no. oo

  • If anyone can test this fiddle on IE8 or 7? http://jsfiddle.net/brasofilo/nthv3g7h/3/

  • Vish, where do you get such an IE? : ) I remembered remote times testing in IE in N versions.

  • @Papacharlie, Bueno, in fact the @teste said that it does not run... or idea where these IE, in a VM of life :P

  • @Brasofilo, from what I’ve seen, will not

  • 3

    @Papacharlie tu tendo o último IE can test in any old version, in a debug option

  • 1

    @Jorge B., true, I just saw v7, V8 and V9. Thanks for the tip, I had no idea this.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.